[Techtalk] foreach won't but for will

Diggy Bell diggy at dbsoftdev.com
Mon Feb 17 17:06:58 EST 2003


I dunno if this will help any, but here's a little info on arrays...

In reality, all arrays in PHP are handled the same way.  In the case of
an array with numeric indices, the associative array tags are simply the
index values.  The foreach() construct will work on either type of
array, but you may not always get the results you expected.

>From what I've been able to tell, PHP uses something like a
doubly-linked list to store arrays.  New elements are added to the tail
of the array, not sorted by index.  That is one very important fact to
consider.

Take this example:

<?php

$test[3] = 3;
$test[1] = 1;
$test[2] = 2;
$test[0] = 0;

foreach($test as $index => $value)
{
   print("[$index] - $value\n");
}

?>

Would you expect it to print out from 0 to 3?  Ooops...here's what it
prints:

[3] - 3
[1] - 1
[2] - 2
[0] - 0

To get this in the desired index order, you would have to use a for()
loop.

for($i = 0; $i < count($test); $i++)
{
}

Now, this can have some implications if you are using multiple arrays
and cross-indexing between them.  Here are some examples that illustrate
what can happen.

?php

//
// these are purposely defined out of index order
//

$values[3] = 3;
$values[1] = 1;
$values[2] = 2;
$values[0] = 0;

//
// these are defined in index order
//

$labels[0] ='Zero';
$labels[1] = 'One';
$labels[2] = 'Two';
$labels[3] = 'Three';

//
// this snippet will traverse the $values array in list order.
// $index is not in sync so the output of $labels is not correct
//

$index = 0;
foreach($values as $value)
{
   print("$labels[$index] - $value\n");
   $index++;
}

print("\n**********\n\n");

//
// this snippet will traverse the $values array in list order
// $index will be in sync so output of $labels will be correct
//

foreach($values as $index => $value)
{
   print("$labels[$index] - $value\n");
}

print("\n**********\n\n");

//
// this snippet will travers the $values array in index order
// $index will be in sync so output of $labels will be correct
//

for($index = 0; $index < count($values); $index++)
{
   print("$labels[$index] - $values[$index]\n");
}

?>

Hope this helps...


On Mon, 2003-02-17 at 12:19, Emma Jane Hogbin wrote:
> I switched the foreach statement to a for($i=0; $i < sizeof($array); $i++)
> and now that part works. I don't really understand why the foreach
> wouldn't work...I was pretty sure that foreach was supposed to work on all
> arrays, but maybe it's only associative arrays?
> 
> I think I may have found it in the PHP documentation.... (go figure):
> /* foreach example 2: value (with key printed for illustration) */
> 
> $a = array (1, 2, 3, 17);
> 
> $i = 0; /* for illustrative purposes only */
> 
> foreach($a as $v) {
>     print "\$a[$i] => $v.\n";
>     $i++;
> }
> 
> Looks like you have to count if it's not an associative array. Bleh. Next
> problem -- to fix the in_array that says it's not.
> 
> emma
-- 
Diggy Bell <diggy at dbsoftdev.com>
DB Software Development




More information about the Techtalk mailing list