Jump to content

array in a array


metamlmini

Recommended Posts

Hi there,

 

I will get straight to the point on this one

 

I have a array in a variable and when i print_r the variable it shows me:

 

Array ( [93] => Array ( [0] => 96 ) ) 

 

How do i print the 93 in the array and the 96? Both these numbers are variable so i would love to see an echo of a variable in this case.

 

(This is all based on code not written by me and the variable structure is a mess, thats why i am asking :-))

 

Thanks in advance!

Link to comment
Share on other sites

think of it this way: A pregnant woman has a baby, and she is in a house.

$house = Array(
          [Mother] =>
                Array (
                         [0] => Baby
                )
)

93 (mother) is a key in an array (house), and the data for 93 IS an array, which contains key 0 (baby), which has a value of 96.

This is a multi-dimensional array. Hopefully that link can help.

Link to comment
Share on other sites

First of all, thanks for the quick reply!

 

Yea i banged my head over this issue yesterday and i came across the website that you pointed at. I did some reading on the multidimensional arrays but i cant seem to figure out how i echo the high numbers that i showed.

 

Array ( [93] => Array ( [0] => 96 ) ) 

 

I your example, how would you echo [mother] (not $row[mother], because in my case i have to work with a variable)

Link to comment
Share on other sites

I'd just like to point out that when your keys are strings, surround them with quotes to ensure PHP doesn't assume them to be constants.

 

echo $array[mother]; // this is bad, php first looks to see if a constant called mother is defined.
echo $array['mother']; // this is good, always surround key with quotes. but only if it's an associative array, that is, the keys are named.

 

Now. What is the end result of these variables? If you are going to do something with them inside a loop, then something like this may suffice.

foreach($filters as $key => $value){
   echo $key; // will echo 93
   echo $value[0]; // will echo 96
}

 

Of course, if you have an array that is deeper you'll need to use a recursive function.

 

Hope this helps a bit.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.