[PHP] PHP bug within multi. dimensional arrays?

2005-06-06 Thread Merlin

Hi there,

I am outputting an multidim. array. That works fine, except one thing. The first 
letter of the value inside dimension 1 always gets printed.


For example:

I fill the arrays:
while ($row = mysql_fetch_object($result)){ 
$cat[$row-main_id][name]= $row-main_name;   
$cat[$row-main_id][$row-sub_id][name] = $row-sub_name;   
   
}

Then I output them:
foreach ($cat AS $maincat){
echo $maincat[name].':';
foreach($maincat AS $subcat){
echo $subcat[name].$br;
}
echo $br;
}

Which does result in:

Europe:E
Germany
UK

North America:N
US
CA

As you can see I get the extra letters N and E. Is this an php error or did I do 
something wrong?


Thanx for any hint,

Merlin

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



Re: [PHP] PHP bug within multi. dimensional arrays?

2005-06-06 Thread Richard Davey
Hello Merlin,

Monday, June 6, 2005, 2:51:39 PM, you wrote:

M while ($row = mysql_fetch_object($result)){
M $cat[$row-main_id][name]   = $row-main_name;
M $cat[$row-main_id][$row-sub_id][name] =
M $row-sub_name;   
M }

Quote array keys.. ALWAYS quote them:

$cat[$blah]['name'] = $whatever

Otherwise PHP will think [name] is trying to use a constant and
substitute a value accordingly.

M foreach ($cat AS $maincat){
M echo $maincat[name].':';
M foreach($maincat AS $subcat){
M echo $subcat[name].$br;
M }
M echo $br;
M }

M As you can see I get the extra letters N and E. Is this an php
M error or did I do something wrong?

You did something wrong ;) If you don't quote the array keys it'll
assume a constant, which in turn would assume a 1 which will give you
the first character of the string.

Best regards,

Richard Davey
-- 
 http://www.launchcode.co.uk - PHP Development Services
 I do not fear computers. I fear the lack of them. - Isaac Asimov

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



Re: [PHP] PHP bug within multi. dimensional arrays?

2005-06-06 Thread Merlin

Richard Davey wrote:

Hello Merlin,

Monday, June 6, 2005, 2:51:39 PM, you wrote:

M while ($row = mysql_fetch_object($result)){
M $cat[$row-main_id][name]   = $row-main_name;
M $cat[$row-main_id][$row-sub_id][name] =
M $row-sub_name;   
M }


Quote array keys.. ALWAYS quote them:

$cat[$blah]['name'] = $whatever

Otherwise PHP will think [name] is trying to use a constant and
substitute a value accordingly.

M foreach ($cat AS $maincat){
M echo $maincat[name].':';
M foreach($maincat AS $subcat){
M echo $subcat[name].$br;
M }
M echo $br;
M }

M As you can see I get the extra letters N and E. Is this an php
M error or did I do something wrong?

You did something wrong ;) If you don't quote the array keys it'll
assume a constant, which in turn would assume a 1 which will give you
the first character of the string.

Best regards,

Richard Davey


Hi Richard,

that sounds absolutly plausible and I now qoted it the way you recommended. 
However I do get the same result. Still the first letter.


Any other ideas?

Thank you in advance,
Merlin

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



Re: [PHP] PHP bug within multi. dimensional arrays?

2005-06-06 Thread Richard Lynch
On Mon, June 6, 2005 6:51 am, Merlin said:
 I am outputting an multidim. array. That works fine, except one thing. The
 first
 letter of the value inside dimension 1 always gets printed.

 For example:

 I fill the arrays:
 while ($row = mysql_fetch_object($result)){
   $cat[$row-main_id][name]   = $row-main_name;

For the record, name does not turn into 0, 1, nor TRUE here, but simply
becomes 'name' which is what you should have typed in the first place.

Set error_reporting(E_ALL) to get more details.

   $cat[$row-main_id][$row-sub_id][name] = $row-sub_name;
 }

 Then I output them:
 foreach ($cat AS $maincat){
   echo $maincat[name].':';

echo maincat is: '$maincat'br /\n;

   foreach($maincat AS $subcat){

echo subcat is: '$subcat'br /\n;

   echo $subcat[name].$br;
   }
   echo $br;
 }

 Which does result in:

 Europe:E

You are passing 'Europe' as $subcat, and then trying to treat it like an
array when you do $subcat['name'].

PHP will let you treat a string as an array of characters, if you insist.

At this point, PHP *does* turn 'name' into the index 0 because you can
only index strings as character arrays using integer indices.

$subcat['name'] = 'Europe'['name'] = 'Europe'[0] = 'E'

 Germany
 UK

 North America:N
 US
 CA

 As you can see I get the extra letters N and E. Is this an php error or
 did I do
 something wrong?



-- 
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