Please consider this code
var_export ($dates);
while (list($key, $date) = each($dates))
{
echo("current = ".current($dates));
echo("key = " . key($dates));
}
result is
Array
(
[1359928800] => 1359928800
)
current =
key =
I expected it should return 1359928800
, where I am wrong?
Why not make use of $key
and $date
?
while (list($key, $date) = each($dates))
{
echo("current = ".$date); // 1359928800
echo("key = " . $key); // 1359928800
}
When working with arrays there is a different less archaic construct for handling iterations: foreach (documentation here).
I'd recommend iterating over the array in this manner. It's much easier to read and nearly impossible to get wrong. In addition, you don't have to worry about the possibility of ending up in an infinite loop as mentioned in the Caution here.
<?php
var_export($dates);
foreach($dates as $key => $value) {
echo("current = ".$value);
echo("key = ".$key);
}
The reason is that each
already advances the pointer.
The documentation states:
Return the current key and value pair from an array and advance the array cursor.
So inside the loop current
refers to the next element. In your case there is no next element, so it's false
. You should use $key
and $date
or better, use foreach
, like already suggested.