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

2005-06-07 Thread Jochem Maas

Matthew Weier O'Phinney wrote:

* Merlin [EMAIL PROTECTED]:


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



First off, if you're creating associative arrays, you should quote the
keys:

$cat[$row-main_id]['name'] = $row-main_name;

If you don't do so, PHP assumes you're using a constant value for the
key name.



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



Quote your keys!



foreach($maincat AS $subcat){



You do realize that the above will also loop over the index 'name',
right?...



echo $subcat[name].$br;



and since it does, the first element in that array is 'name', which
isn't an array, but a string. Since the 'name' constant isn't defined,
it will interpret that as 'true', or 1, and so it takes the first
character of that string.


I think you'll find the 'name' constant evaluates to FALSE, which in turn
casts to zero, which will give you the first element of the given array,
but as you pointed out its a string not an array so you get the first
char (because php allows array-like access to the individual chars in
a string) ... if it had evaluated to TRUE you would be getting the second
char.

the rest of your explaination is spot on.





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



So, what you should probably do is create an additional layer in your
multi-dimensional array for the subcategories, and have it of the form
sub_id = sub_name:

$cat[$row-main_id]['subs'][$row-sub_id] = $row-sub_name;

Then loop over that:

foreach ($cat as $main_cat) {
echo $maincat['name'] . :\n;
foreach ($maincat['subs'] as $sub_id = $sub_name) {
echo $sub_name$br; // could also use $sub_id here if
 // desired
}
}



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



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

2005-06-06 Thread Matthew Weier O'Phinney
* Merlin [EMAIL PROTECTED]:
 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;   
 
 }

First off, if you're creating associative arrays, you should quote the
keys:

$cat[$row-main_id]['name'] = $row-main_name;

If you don't do so, PHP assumes you're using a constant value for the
key name.

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

Quote your keys!

   foreach($maincat AS $subcat){

You do realize that the above will also loop over the index 'name',
right?...

   echo $subcat[name].$br;

and since it does, the first element in that array is 'name', which
isn't an array, but a string. Since the 'name' constant isn't defined,
it will interpret that as 'true', or 1, and so it takes the first
character of that string.

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

So, what you should probably do is create an additional layer in your
multi-dimensional array for the subcategories, and have it of the form
sub_id = sub_name:

$cat[$row-main_id]['subs'][$row-sub_id] = $row-sub_name; 

Then loop over that:

foreach ($cat as $main_cat) {
echo $maincat['name'] . :\n;
foreach ($maincat['subs'] as $sub_id = $sub_name) {
echo $sub_name$br; // could also use $sub_id here if
 // desired
}
}

-- 
Matthew Weier O'Phinney   | WEBSITES:
Webmaster and IT Specialist   | http://www.garden.org
National Gardening Association| http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:[EMAIL PROTECTED] | http://vermontbotanical.org

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



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

2005-06-06 Thread Merlin

Matthew Weier O'Phinney wrote:

* Merlin [EMAIL PROTECTED]:


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



First off, if you're creating associative arrays, you should quote the
keys:

$cat[$row-main_id]['name'] = $row-main_name;

If you don't do so, PHP assumes you're using a constant value for the
key name.



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



Quote your keys!



foreach($maincat AS $subcat){



You do realize that the above will also loop over the index 'name',
right?...



echo $subcat[name].$br;



and since it does, the first element in that array is 'name', which
isn't an array, but a string. Since the 'name' constant isn't defined,
it will interpret that as 'true', or 1, and so it takes the first
character of that string.



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



So, what you should probably do is create an additional layer in your
multi-dimensional array for the subcategories, and have it of the form
sub_id = sub_name:

$cat[$row-main_id]['subs'][$row-sub_id] = $row-sub_name;

Then loop over that:

foreach ($cat as $main_cat) {
echo $maincat['name'] . :\n;
foreach ($maincat['subs'] as $sub_id = $sub_name) {
echo $sub_name$br; // could also use $sub_id here if
 // desired
}
}



This is very helpful and does work. However I did not understand it completley. 
What if I want to add another value, for example name2 or name3.

It looks like this example is limited to id and name.
Could I just add:
$cat[$row-main_id]['subs'][$row-sub_id] = $row-name2;   

Guess not. Can you tell me how to add other fields to the array?

Thank you in advance, Merlin

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



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

2005-06-06 Thread Matthew Weier O'Phinney
* Merlin [EMAIL PROTECTED] :
 Matthew Weier O'Phinney wrote:
  * Merlin [EMAIL PROTECTED] :
   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;
   
   }
  
  First off, if you're creating associative arrays, you should quote the
  keys:
  
  $cat[$row- main_id]['name'] = $row- main_name;
  
  If you don't do so, PHP assumes you're using a constant value for the
  key name.
  
   Then I output them:
   foreach ($cat AS $maincat){
 echo $maincat[name].':';
  
  Quote your keys!
  
 foreach($maincat AS $subcat){
  
  You do realize that the above will also loop over the index 'name',
  right?...
  
  
 echo $subcat[name].$br;
  
  and since it does, the first element in that array is 'name', which
  isn't an array, but a string. Since the 'name' constant isn't defined,
  it will interpret that as 'true', or 1, and so it takes the first
  character of that string.
  
  
 }
 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?
  
  So, what you should probably do is create an additional layer in your
  multi-dimensional array for the subcategories, and have it of the form
  sub_id = sub_name:
  
  $cat[$row- main_id]['subs'][$row- sub_id] = $row- sub_name;  
  
  Then loop over that:
  
  foreach ($cat as $main_cat) {
  echo $maincat['name'] . :\n;
  foreach ($maincat['subs'] as $sub_id = $sub_name) {
  echo $sub_name$br; // could also use $sub_id here if
   // desired
  }
  }
  

 This is very helpful and does work. However I did not understand it 
 completley. 
 What if I want to add another value, for example name2 or name3.
 It looks like this example is limited to id and name.
 Could I just add:
 $cat[$row- main_id]['subs'][$row- sub_id] = $row- name2;   

 Guess not. Can you tell me how to add other fields to the array?

You should probably do some studying up on multidimensional and/or
nested arrays so you can get a better handle on this stuff.

If you need to be able to add multiple names for a sub_id, do it as an
array:

$cat[$row-main_id]['subs'][$row-sub_id][] = $row-name1;
$cat[$row-main_id]['subs'][$row-sub_id][] = $row-name2;
$cat[$row-main_id]['subs'][$row-sub_id][] = $row-name3;

However, this will break your loop above -- you'll need another layer of
looping added in:

foreach ($cat as $main_cat) {
echo $maincat['name'] . :\n;
foreach ($maincat['subs'] as $sub_id = $names) {
foreach ($names as $name) {
echo $name$br; // could also use $sub_id here if
 // desired
}
}
}

Hope that helps.

-- 
Matthew Weier O'Phinney   | WEBSITES:
Webmaster and IT Specialist   | http://www.garden.org
National Gardening Association| http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:[EMAIL PROTECTED] | http://vermontbotanical.org

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