printJimy Posted November 26, 2012 Share Posted November 26, 2012 Hello . ( explain me plz)How can i output an Multidimensional array with for loop(or while), not with foreach ? example this array $exampleMultidmArr = array ( "nested1" =>array (1,2,3,4,5), "nested2" =>array (7,20,43,44,25), ); Thank You. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/ Share on other sites More sharing options...
krisw44 Posted November 26, 2012 Share Posted November 26, 2012 You could do it with a 'for' loop or a 'foreach' loop. I think it is more a matter of preference and what you are going to do with the data. I tend to use 'for' loops. for(i=0; i < count($exampleMultidmArr); i++) { for(j=0; j < count($exampleMultidmArr[i]); j++) { echo $exampleMultidmArr[i][j]; } } With this method you can do additional things with the data since you are determining the exact position of each item. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395285 Share on other sites More sharing options...
Christian F. Posted November 26, 2012 Share Posted November 26, 2012 If you have a large array, I would recommend putting the count () outside of the for loop. You'll save about 33% on it, with ~0.02 ms vs ~0.03 ms with 100k iterations, on my test system. So not a whole lot, but it can be noticeable on huge arrays. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395294 Share on other sites More sharing options...
AyKay47 Posted November 26, 2012 Share Posted November 26, 2012 Just for shiggles, here is an example using while loops: $mArr = array( 0=>array(1, 2, 3, 4, 5), 1=>array(7, 20, 43, 44, 25) ); $i = 0; $c = count($mArr); while($i < $c) { $j=0; $c2 = count($mArr[$i]); while($j < $c2) { echo $mArr[$i][$j] . PHP_EOL; $j++; } $i++; } But, using a foreach loop is best. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395317 Share on other sites More sharing options...
Jessica Posted November 26, 2012 Share Posted November 26, 2012 print_r is my favorite PHP function. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395321 Share on other sites More sharing options...
printJimy Posted November 27, 2012 Author Share Posted November 27, 2012 Thank to all of you , i really understood issue now . Also when we have the key of array as a string , we need to use a string also in a loop , example like this ? is it the right ? <?php $mArr = array( "a"=>array(1, "string", 3, 4, 5), "b"=>array(7, 20, 43, 44, 25) ); for ($i="a"; $i<= "b"; $i++) { for ($j=0;$j<count($mArr[$i]); $j++) { echo $mArr[$i][$j]." "; } echo"<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395466 Share on other sites More sharing options...
Muddy_Funster Posted November 27, 2012 Share Posted November 27, 2012 neither of those two arrays have a string key in them Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395500 Share on other sites More sharing options...
AyKay47 Posted November 27, 2012 Share Posted November 27, 2012 neither of those two arrays have a string key in them "a" and "b" are both string keys. In a situation where you would need to have string keys and do not want to use a foreach loop (why not?), use a persistent naming convention for the keys so you can reference them properly inside of the loop(s). Something like this: $mArr = array( "number1"=>array(1, 2, 3, 4, 5), "number2"=>array(7, 20, 43, 44, 25) ); $i = 0; $c = count($mArr); while($i < $c) { $index = "number" . ($i + 1); $j=0; $c2 = count($mArr[$index]); while($j < $c2) { echo $mArr[$index][$j] . PHP_EOL; $j++; } $i++; } Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395523 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2012 Share Posted November 27, 2012 You can use array_keys to get the keys into an enumerated array. Then use a for() loop regardless of the values or order of the original keys. Using foreach() would still be a much better solution though, IMO. Iterating over arrays is exactly what it's there for. $mArr = array( "a"=>array(1, "string", 3, 4, 5), "b"=>array(7, 20, 43, 44, 25) ); $keys = array_keys($mArr); $c = count($keys); for( $i = 0; $i < $c; $i++ ) { print_r( $mArr[$keys[$i]] ); echo '<br>'; } Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395532 Share on other sites More sharing options...
Iluvatar+ Posted November 27, 2012 Share Posted November 27, 2012 Why not foreach? Hello . ( explain me plz)How can i output an Multidimensional array with for loop(or while), not with foreach ? example this array $exampleMultidmArr = array ( "nested1" =>array (1,2,3,4,5), "nested2" =>array (7,20,43,44,25), ); Thank You. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395552 Share on other sites More sharing options...
printJimy Posted November 27, 2012 Author Share Posted November 27, 2012 I'm just learning how to use" for or while" in these situations . again , thank you too much. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395610 Share on other sites More sharing options...
printJimy Posted November 28, 2012 Author Share Posted November 28, 2012 also i have a question about how to output the key of array with the for loop . example this is an array and this array have 2 key 1 and 2 , how to output these keys . $vegetables=array (1=>"apples", 2=>"pear"); and the output of this array i like to be (if it is possible) : 1 apples 2 pear without any special function like key(); or something like this . Thanks Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395896 Share on other sites More sharing options...
Christian F. Posted November 28, 2012 Share Posted November 28, 2012 Read up on foreach (), and you'll see. Also, key () isn't a special function in any way, it's been a part of the core PHP functionality since at least 4.x. Granted, it's not often used as foreach () is often the better alternative. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395898 Share on other sites More sharing options...
printJimy Posted November 28, 2012 Author Share Posted November 28, 2012 Read up on foreach (), and you'll see. Also, key () isn't a special function in any way, it's been a part of the core PHP functionality since at least 4.x. Granted, it's not often used as foreach () is often the better alternative. i know with foreach , foreach ($vegetables as $k => $v) { echo $k; } , but i want with "for" . Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395902 Share on other sites More sharing options...
Christian F. Posted November 28, 2012 Share Posted November 28, 2012 Then you have to use key () as well as next (), or each (). However, in almost every case when you want the key and the value, you'll want to use foreach (). So if this is only for training: Keep it up. Just don't try to avoid foreach () in production code, just "because". Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395904 Share on other sites More sharing options...
Barand Posted November 28, 2012 Share Posted November 28, 2012 i know with foreach , ... but i want with "for" . And would you knock screws in with a hammer, or do the sensible thing and use a screwdriver? Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395930 Share on other sites More sharing options...
printJimy Posted November 28, 2012 Author Share Posted November 28, 2012 And would you knock screws in with a hammer, or do the sensible thing and use a screwdriver? Ok , i said that im just learning how to use other loop in these case , but i do not use" for or while" when i work with array , because i know that foreach is constructed for that. So i just want to know how to use other loop to manipulate with array. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395942 Share on other sites More sharing options...
Jessica Posted November 28, 2012 Share Posted November 28, 2012 That makes no sense. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395946 Share on other sites More sharing options...
Beeeeney Posted November 28, 2012 Share Posted November 28, 2012 Ok , i said that im just learning how to use other loop in these case , but i do not use" for or while" when i work with array , because i know that foreach is constructed for that. So i just want to know how to use other loop to manipulate with array. There isn't really a reason that you shouldn't be using foreach for your purpose. I think you're just being awkward. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395949 Share on other sites More sharing options...
printJimy Posted November 28, 2012 Author Share Posted November 28, 2012 ok ok, I will not use never again "for or while" . Thank to all of you , topic can be closed . Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395951 Share on other sites More sharing options...
Beeeeney Posted November 28, 2012 Share Posted November 28, 2012 ok ok, I will not use never again "for or while" . Thank to all of you , topic can be closed . To say you'll never use them again is a bit much, isn't it? I don't want you to leave this forum thinking that for loops and while loops are bad form. Use them for their intended purposes! Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395954 Share on other sites More sharing options...
printJimy Posted November 28, 2012 Author Share Posted November 28, 2012 I do not say to leave forum , Now I understood the issue , and i thought not to use a " for " in these situations , not forever(even i said ). I am here to learn, not to oppose . Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395966 Share on other sites More sharing options...
Beeeeney Posted November 28, 2012 Share Posted November 28, 2012 If I understand what you just said, and I think I do, then that's good stuff. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395969 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2012 Share Posted November 28, 2012 Since you already know foreach() is there for dealing with arrays, if using a for() or while() loop on an array helps you learn how they work, then by all means do it. Link to comment https://forums.phpfreaks.com/topic/271208-how-to-output-multidimensional-array-with-for-loop/#findComment-1395974 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.