Last active
August 29, 2015 14:10
-
-
Save chartjes/3b4c4814a5a0ec8f4904 to your computer and use it in GitHub Desktop.
Fun with PHP Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given the following code | |
function foo(array $bar) { | |
$barCount = count($bar); | |
for ($i = 0; $i < $barCount; $i++) { | |
$baz = explode(Validator::DELIMITER, $bar[$i]); | |
unset($bar[$i]); | |
foreach ($bar as $val) { | |
$val = trim($val); | |
if ($val !== '') { | |
$bar[] = $val; | |
} | |
} | |
} | |
return $bar; | |
} | |
// What would have to be in $bar for it to trigger an | |
// "offset does not exist error" on the line with explode? |
Yeah I also think foreach would solve it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use a foreach on the outer loop? unsetting inside a foreach is safe (IIRC)