chris57828 Posted May 8, 2011 Share Posted May 8, 2011 Hi everyone, If you take a look at my code below I am having trouble echoing the key/ value pair of the 'sex' array which is a sub array of 'pens'. I have tried so many different ways but to no avail, thanks Chris! <?php $products = array( 'paper' => array( 'copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"), 'pens' => array( 'ball' => "Ball Point", 'hilite' => "Highlighters", 'marker' => "Markers", 'sex' => array ( 'condom' => "Protection")), 'misc' => array( 'tape' => "Sticky Tape", 'glue' => "Adhesives", 'clips' => "Paperclips") ); echo "<pre>"; foreach ($products as $section => $items ) foreach ($items as $key => $value) echo "$section:\t$key\t($value)<br>"; echo "</pre>"; ?> :'( Quote Link to comment https://forums.phpfreaks.com/topic/235860-multidimensional-arrays-foreachloop/ Share on other sites More sharing options...
wildteen88 Posted May 8, 2011 Share Posted May 8, 2011 You'll need to use a third foreach loop to echo the key/value pairs for the $products['pens']['sex'] array foreach ($products as $section => $items) { foreach ($items as $key => $value) { echo "$section:\t$key\t($value)<br>"; // check whether the current item is an array! if(is_array($value)) { echo "$key is sub array:<br />"; foreach($value as $subKey => $subValue) echo "$subKey:\t$subValue<br />"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/235860-multidimensional-arrays-foreachloop/#findComment-1212415 Share on other sites More sharing options...
chris57828 Posted May 8, 2011 Author Share Posted May 8, 2011 Thanks babe. this forum is so cooooooooooooooool Quote Link to comment https://forums.phpfreaks.com/topic/235860-multidimensional-arrays-foreachloop/#findComment-1212417 Share on other sites More sharing options...
sraj Posted February 25, 2013 Share Posted February 25, 2013 (edited) Hi , Could you please help me to sort the array of array['laser'] to top. Thank in advance. <?php $products = array( 'paper' => array( 'copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"), 'pens' => array( 'copier' => "Ball Point", 'inkjet' => "Highlighters", 'laser' => "Markers", 'photo' => "Protection")), 'misc' => array( 'copier' => "Sticky Tape", 'inkjet' => "Adhesives", 'laser' => "Sweet", 'photo' => "Protection")); echo "<pre>"; foreach ($products as $section => $items ) foreach ($items as $key => $value) echo "$section:\t$key\t($value)<br>"; echo "</pre>"; ?> Edited February 25, 2013 by sraj Quote Link to comment https://forums.phpfreaks.com/topic/235860-multidimensional-arrays-foreachloop/#findComment-1414801 Share on other sites More sharing options...
cyberRobot Posted February 25, 2013 Share Posted February 25, 2013 Could you please help me to sort the array of array['laser'] to top. Thank in advance. You could try usort: http://php.net/manua...ction.usort.php Quote Link to comment https://forums.phpfreaks.com/topic/235860-multidimensional-arrays-foreachloop/#findComment-1414808 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.