note 32814 added to ref.array
| From: | jon at pixelmajik dot com | Date: | Sat, 07 Jun 2003 22:34:42 +0000 |
| Subject: | note 32814 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-49958@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32814
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32814&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32814&report=yes