Date: 10/02/00
- Next message: NickM: "RE: [phplib] Array Insanity"
- Previous message: Peter Karsten: "[phplib] phplib"
- Next in thread: NickM: "RE: [phplib] Array Insanity"
- Reply: NickM: "RE: [phplib] Array Insanity"
- Maybe reply: Matthew Leingang: "RE: [phplib] Array Insanity"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
does this same advice apply to the use of phplib?
i.e. - $db->f(ID) vs. $db->f('ID') ?
Thanks for the advice.
-----Original Message-----
From: Matthew Leingang [mailto:leingang <email protected>]
Sent: Saturday, September 30, 2000 4:42 PM
To: Holger Blasum
Cc: Rex Byrns; Phplib <email protected> Netuse. De (E-mail)
Subject: Re: [phplib] Array Insanity
Hello!
May I add (Rex) that your associative array keys (Parent, Name, etc...)
should be quoted so the parser won't confuse them with constants or
reserved words.
----------------------------------------------------------------
Matthew Leingang http://www.math.rutgers.edu/
Rutgers University leingang <email protected>
Department of Mathematics "This signature needs no quote."
On Sat, 30 Sep 2000, Holger Blasum wrote:
> 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.
>
> [ Part 11: "Included Message" ]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>
- Next message: NickM: "RE: [phplib] Array Insanity"
- Previous message: Peter Karsten: "[phplib] phplib"
- Next in thread: NickM: "RE: [phplib] Array Insanity"
- Reply: NickM: "RE: [phplib] Array Insanity"
- Maybe reply: Matthew Leingang: "RE: [phplib] Array Insanity"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

