help with multidimentional arrays
| From: | Bing Du | Date: | Tue, 11 Apr 2006 18:58:23 +0000 |
| Subject: | help with multidimentional arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-233788@lists.php.net to get a copy of this message | ||
Hello,
What I intend to do is put the database query results in a
multidimentional array like this.
$sponsor_id['sponsor1'] = ('project1 title', 'project2 title',
'project3
title');
$sponsor_id['sponsor2'] = ('project1 title','project7 title');
....
Here is the code snippet for doing that:
==
while( ($rec = odbtp_fetch_array($qry)) ) {
if (empty($sponsor_id[$rec[1]])) {
$sponsor_id[$rec[1]] = array();
} else {
array_push($sponsor_id[$rec[1]], $rec[0]);
}
}
==
Now, when the following code is used to print the array $sponsor_id, it
only prints out the keys of the array which are:
sponsor1
sponsor2
Those project titles are not printed out.
==
foreach ($sponsor_id as $sponsor => $arr)
echo "$sponsor:";
foreach ($arr[$sponsor] as $project) {
echo "$project<br>";
}
==
My expected output should be like:
sponsor1:
project1 title
project2 title
project3 title
sponsor2
project1 title
project7 title
What is wrong? I'd appreciate any help.
Thanks,
Bing