unemployment Posted February 21, 2011 Share Posted February 21, 2011 How do I echo the array below in a for each loop? I'm trying to output this data in a table. Array ( [u] => Array ( [0] => Array ( [row_id] => 1 [0] => Jn [1] => Bo [2] => do [3] => jilcom [4] => hocom ) [1] => Array ( [row_id] => 3 [0] => Jk [1] => Kk [2] => wd [3] => jk [4] => ) [2] => Array ( [row_id] => 5 [0] => Jn [1] => Bo [2] => on [3] => jm [4] => hm ) ) [blog] => Array ( [0] => Array ( [row_id] => 3 [0] => dk [1] => [2] => [3] => [4] => ) [1] => Array ( [row_id] => 9 [0] => 2rs [1] => dee [2] => [3] => [4] => ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/ Share on other sites More sharing options...
denno020 Posted February 21, 2011 Share Posted February 21, 2011 $array = array(array(1, item1), array(2, item2), array(3, item3)); for($i ; $i < ($array length) ; $i ++){ for($j ; $j < ($array length) ; $j++){ echo $array[$i][$j]; } } Something similar to that. I'm not sure how to get the array length, which is why i've just written $array length, but it shouldn't be too hard to figure that bit out.. Basically you want a for loop for the index part of the array, and then an inner for loop for all the things associated with that index. Hope that helps Denno Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177409 Share on other sites More sharing options...
btherl Posted February 21, 2011 Share Posted February 21, 2011 foreach ($array as $thing => $subarr) { echo "$thing: "; foreach ($subarr as $data) { echo $data['row_id']; echo $data[0]; } } This should work. I've echoed some items to show how to reference them, there is no formatting of the output. Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177410 Share on other sites More sharing options...
btherl Posted February 21, 2011 Share Posted February 21, 2011 Denno, the OP's top level array is indexed by strings rather than by numbers. In general you should use "foreach" instead of "for" in php, unless you have a special reason to be using "for". Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177411 Share on other sites More sharing options...
denno020 Posted February 21, 2011 Share Posted February 21, 2011 Denno, the OP's top level array is indexed by strings rather than by numbers. In general you should use "foreach" instead of "for" in php, unless you have a special reason to be using "for". Yeah sorry bout that.. I'm not familiar with OOP stuff, so I don't really think in those terms yet.. Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177412 Share on other sites More sharing options...
unemployment Posted February 21, 2011 Author Share Posted February 21, 2011 Can anyone give me an example using my arrays? Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177414 Share on other sites More sharing options...
btherl Posted February 21, 2011 Share Posted February 21, 2011 unemployment, the example I gave was for your arrays. Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177429 Share on other sites More sharing options...
sasa Posted February 21, 2011 Share Posted February 21, 2011 <?php $test = Array ( 'u' => Array ( '0' => Array ( 'row_id' => '1', '0' => 'Jn', '1' => 'Bo', '2' => 'do', '3' => 'jilcom', '4' => 'hocom' ), '1' => Array ( 'row_id' => 3, '0' => 'Jk', '1' => 'Kk', '2' => 'wd', '3' => 'jk', '4' => '' ), '2' => Array ( 'row_id' => 5, '0' => 'Jn', '1' => 'Bo', '2' => 'on', '3' => 'jm', '4' => 'hm' ) ), 'blog' => Array ( '0' => Array ( 'row_id' => 3, '0' => 'dk', '1' => '', '2' => '', '3' => '', '4' => '', ), '1' => Array ( 'row_id' => 9, '0' => 'rs', '1' => 'dee', '2' => '', '3' => '', '4' => '', ) ) ); echo "<table border=3>\n"; foreach ($test as $key => $test1){ echo "<tr><td colspan=6>$key</td></tr>\n"; $start = true; foreach ($test1 as $key => $row){ if($start){ echo "<tr><td>". implode('</td><td>',array_keys($row)),"</td></tr>\n"; $start = false; } echo "<tr><td>". implode('</td><td>',$row),"</td></tr>\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177494 Share on other sites More sharing options...
cunoodle2 Posted February 21, 2011 Share Posted February 21, 2011 Denno, the OP's top level array is indexed by strings rather than by numbers. In general you should use "foreach" instead of "for" in php, unless you have a special reason to be using "for". Is there a reason to lean on foreach vs for? I've tried to use foreach many times and for some reason I just cannot wrap my head around it. Is it a performance/security issue? Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177620 Share on other sites More sharing options...
btherl Posted February 21, 2011 Share Posted February 21, 2011 Denno, the OP's top level array is indexed by strings rather than by numbers. In general you should use "foreach" instead of "for" in php, unless you have a special reason to be using "for". Is there a reason to lean on foreach vs for? I've tried to use foreach many times and for some reason I just cannot wrap my head around it. Is it a performance/security issue? It's more of a flexibility issue. "for" will only work for arrays indexed by ordered integers, beginning at the expected number and with no gaps. For example if your indexes are "0, 1, 2, 4, 5, 6, 7" because you deleted the item with index #3, then "for" will no longer work as expected. The end result is you have to keep renumbering your arrays in order for them to work with "for". But foreach works with ANY array. It can be indexed by words, numbers with gaps, mixed numbers and words, anything. There's no need to renumber arrays after removing elements because foreach will skip them automatically. The only situation I use "for" is if I have a special reason to use ordered integer indexes in my arrays, and it's important that those indexes match up. For example if my array is storing objects at coordinates on a grid, and the indexes are the X and Y coordinates, then "for" will work better than "foreach". The other good thing about foreach is that it automatically handles the situation where your array has variable size. With "for" you need to know how long your array is, eg "for ($i = 0; $i < $array_length; $i++)", vs "foreach ($array as $item)". Quote Link to comment https://forums.phpfreaks.com/topic/228339-echo-multidimentional-arrays/#findComment-1177955 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.