Iterating over an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shihab-alain
Forum Newbie
Posts: 2
Joined: Mon Nov 10, 2008 4:45 pm

Iterating over an array

Post by shihab-alain »

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:
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];
   }
?>
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?

thanks,
Sam
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Iterating over an array

Post by Stryks »

It depends on how the array is generated.

Looping over

Code: Select all

$myArray[] = $somevalue;
... will leave them all your elements in order.

But then, what about if I did this ...

Code: Select all

$myArray = array(0=>'test', 1=>'test', 'BOB'='test', 101=>'test');
... or even ...

Code: Select all

$myArray = array(0=>'test', 1=>'test', 2='test', 3=>'test');
unset($myArray[2]);
Your code will fall over.

Whereas, this ...

Code: Select all

foreach($myArray as $element) print $element;
 
// or
 
foreach($myArray as $key=>$element) print $key .' = ' . $element;
... will work for ell elements every time.

Hope that clarifies somewhat.
User avatar
shihab-alain
Forum Newbie
Posts: 2
Joined: Mon Nov 10, 2008 4:45 pm

Re: Iterating over an array

Post by shihab-alain »

That totally makes sense and is somewhat along the lines of what I was thinking. I was just a bit confused because the php tutorial made it seem like that this could be the case for an array where you do not assign the key and do not delete any of the elements.

thanks,
Sam
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Iterating over an array

Post by Stryks »

I think what the tutorial is really trying to suggest is that the best approach to take is to not make any assumptions about the structure of an array. It's good advice I think, as there are numerous functions that can alter array ordering.

For me, I've written entire apps without using for(). I'm much more of a fan of foreach(), simply because I know I can throw any array I want at it and it will iterate them in order (internal order, not numeric) regardless of the key being in order, contiguous, or even numeric.

Cheers
Post Reply