note 32814 deleted from ref.array by sniper

From: Date: Sat, 18 Oct 2003 18:00:49 +0000
Subject: note 32814 deleted from ref.array by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58821@lists.php.net to get a copy of this message
Note Submitter: jon@pixelmajik.com ---- Re: Frank's note dated 09-Mar-2003 The problem you are demonstrating is not a problem with the behaviour of multidimensional arrays, but rather with a misunderstanding of how $array[]=value works. It is essentially equivalent to an array_push, so when you do $a[][0]=1; $a[][1]=2; $a[][0]=3; $a[][1]=4; instead of your desired structure $a[0] = array(1,2); $a[1] = array(3,4); you end up with $a[0][0] = 1; $a[1][1] = 2; $a[2][0] = 3; $a[3][1] = 4; thus your foreach loop does not behave as expected. Generally I find it easier in your described use to just do while($row = ...fetch_array(...)) { $myarray[] = $row; } thus ending up with an array of hashes. If you really wanted an array of arrays, you could use ...fetch_row (assuming pg_fetch* behaves like mysql_fetch*) or you could keep a counter manually specifying the index of $myarray.

« previous php.notes (#58821) next »