Skip to content

Instantly share code, notes, and snippets.

@bootjp
Last active August 29, 2015 14:14
Show Gist options
  • Save bootjp/1763c60d479f51757b38 to your computer and use it in GitHub Desktop.
Save bootjp/1763c60d479f51757b38 to your computer and use it in GitHub Desktop.
PHP 5.5.14 foreach 内のポインタの挙動
<?php
$array = [
'key' => 'value',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value4',
'key5' => 'value5'
];
foreach ($array as $key => $value) {
$tmp = current($array); // string(6) "value2"
var_dump($tmp); // string(6) "value2"
$tmp = next($array); // string(6) "value3"
$tmp = prev($array); // string...(略)
if (next($array) === false) { //次がなければreturn …ポインタずれてるやん?
return $value; // これだとポインタが2つ進んでるから…
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment