Re: [PHP] help with multidimentional arrays

2006-04-12 Thread John Wells
On 4/11/06, Bing Du [EMAIL PROTECTED] wrote:
 ==
 foreach ($sponsor_id as $sponsor = $arr)
 echo $sponsor:;
 foreach ($arr[$sponsor] as $project) {
 echo $projectbr;
 }
 ==


It looks like you're building your array just fine.  Here though, your
second foreach needs to look like this:

[code]
foreach ($arr as $project)
{
 echo $projectbr;
}
[/code]

Your temprorary $arr variable contains only your array of titles, not
an entire row of your 2-dimensional $sonsor_id array.  So you do not
need to use the $sponsor key to get at that array.

HTH,

John W

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] help with multidimentional arrays

2006-04-12 Thread Bing Du
 On 4/11/06, Bing Du [EMAIL PROTECTED] wrote:
 = foreach ($sponsor_id as $sponsor = $arr)
 echo $sponsor:;
 foreach ($arr[$sponsor] as $project) {
 echo $projectbr;
 }
 =

 It looks like you're building your array just fine.  Here though, your
 second foreach needs to look like this:

 [code]
 foreach ($arr as $project)
 {
  echo $projectbr;
 }
 [/code]

 Your temprorary $arr variable contains only your array of titles, not
 an entire row of your 2-dimensional $sonsor_id array.  So you do not
 need to use the $sponsor key to get at that array.


Thanks so much for pointing out that error on $arr, John.  You are very
right.  Now I realize it's such an obvious error.  But I did not notice it
before I posted.

Bing

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] help with multidimentional arrays

2006-04-11 Thread Bing Du
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 $projectbr;
}
==

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] help with multidimentional arrays

2006-04-11 Thread Richard Lynch
On Tue, April 11, 2006 1:58 pm, Bing Du wrote:
 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)) ) {

echo rec[1] is: $reg[1]br /\n;

if (empty($sponsor_id[$rec[1]])) {
   $sponsor_id[$rec[1]] = array();
} else {
echo adding $rec[0] to $rec[1] arraybr /\n;
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 $projectbr;
 }
 ==

 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

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php