Date: 09/29/00
- Next message: Daniel Bondurant: "[phplib] tree.inc"
- Previous message: Rex Byrns: "[phplib] Array Insanity"
- In reply to: Rex Byrns: "[phplib] Array Insanity"
- Next in thread: Matthew Leingang: "Re: [phplib] Array Insanity"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sat, Sep 30, 2000 at 12:13:44AM +0200, Rex Byrns wrote:
Rex:
[some code to create an associative array from a database query]
> $garray = array();
(superfluous)
> $garray = array($newarray);
(add a comment here what garray is, so you will remember next month :) )
> $c = count($newarray);
> echo $c." elements in newarray<BR>";
> $i = 0;
> $j = 0;
>
> while ($i <= $c){
$i < $c
> $parent = $newarray[$i][Parent];
> $na = $newarray[$i][Name];
> #echo $na;
> reset($garray);
> $gcount = count($garray);
> $j = 1;
$j = 0;
>
> While ($j <= $gcount){
Here you intended: while $j < $gcount. As in many other languages, array indexing starts with 0. This only does not crash because assoc arrays contains their indices in php.
> if($garray[$j][ID]=$parent){
Here you intended to write if($garray[$j][ID] == $parent) {.
As is, with the single equality sign, this line *always* will evaluate to true in each run. So it is true in the last run (where $j is $gcount). Consequently
the entries of the $garray[$gcount] are always used.
> $newarray[$i][Used]='1';
> $newarray[$i][Pname]= $garray[$j][Name];
> }
> $j++;
> }
> $i++;
> }
>
Using the count-for loop like this on an associatiave array will also run through the indices of the associative array. You can easily gain insight into the structure of an associative array by introspecting it using the "echo serialize ($array);" command. (Playing with serialize is any case highly recommendable for understanding PHP data structures.)
The *much* more common approach for traversing through associative arrays is to use the "while (($key, $value) = each ($array))" construct (no need for counting, for more extensive doc see the miscellaneous each () function).
BTW, that code is not very efficient for large arrays, because if you have eg 10000 entries you can expect to run through it 10000*10000/2 times. Iff that is an issue think of checking using an associative array (whose ordering is automatically optimized for you) directly for the parent-child relationship so you only will have to write "if (isset($array_a[$array_b[$parent]])". Umm, if you are using tree relationships anyhow, there is a PHPLIB tree module (tree.inc) which may be worth looking at.
HTH (admittedly I found the oddities of PHP associative arrays once hard to understand too...) , Holger
Pls sign http://petition.eurolinux.org against software patents in Europe.
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>
- Next message: Daniel Bondurant: "[phplib] tree.inc"
- Previous message: Rex Byrns: "[phplib] Array Insanity"
- In reply to: Rex Byrns: "[phplib] Array Insanity"
- Next in thread: Matthew Leingang: "Re: [phplib] Array Insanity"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

