metamlmini Posted May 12, 2011 Share Posted May 12, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/236188-array-in-a-array/ Share on other sites More sharing options...
jonsjava Posted May 12, 2011 Share Posted May 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236188-array-in-a-array/#findComment-1214329 Share on other sites More sharing options...
metamlmini Posted May 12, 2011 Author Share Posted May 12, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/236188-array-in-a-array/#findComment-1214337 Share on other sites More sharing options...
metamlmini Posted May 12, 2011 Author Share Posted May 12, 2011 Yay! But now for efficiency, how do i loop through these steps? Because there will be more array_keys $filt1 = array_keys($filters); echo $filt1[0]; $filt2 = $filters[$filt1[0]]; echo $filt2[0]; Quote Link to comment https://forums.phpfreaks.com/topic/236188-array-in-a-array/#findComment-1214346 Share on other sites More sharing options...
JasonLewis Posted May 12, 2011 Share Posted May 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236188-array-in-a-array/#findComment-1214388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.