Iterating over an array
Posted: Mon Nov 10, 2008 8:41 pm
Hey all,
I was reading a php tutorial called "Practical PHP Programming" at http://hudzilla.org/phpwiki/index.php?t ... ver_arrays and one of the articles stated that:
thanks,
Sam
I was reading a php tutorial called "Practical PHP Programming" at http://hudzilla.org/phpwiki/index.php?t ... ver_arrays and one of the articles stated that:
However, I don't think I've ever found this to be the case. Does anyone know if this is true and, if so, why? Does anyone have any examples of this happening?If you do not specify a key, as in the first example, PHP will just assign incrementing numbers. However, these numbers cannot be guaranteed to exist within the array in any given order, or even to exist at all – they are just key values themselves. For example, an array may have keys 0, 1, 2, 5, 3, 6, 7. That is, it can have its keys out of order or entirely missing. As a result, code like this should generally be avoided:
Code: Select all
<?php for ($i = 0; $i < count($array); ++$i) { print $array[$i]; } ?>
thanks,
Sam