dadamssg87 Posted July 21, 2011 Share Posted July 21, 2011 I'm trying to order a foreach based on a certain item in the array of an array. So like <?php foreach($room as $key => $value) { if($room[$key]['available'] == "Open") { //code to display array }else { //wait till all "Open" items are displayed and then display non-"open" items } } anybody have any suggestions on the best way to tackle this? Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/ Share on other sites More sharing options...
AyKay47 Posted July 21, 2011 Share Posted July 21, 2011 depends on what you are trying to do here, are you having issues with this script? Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/#findComment-1245880 Share on other sites More sharing options...
teynon Posted July 21, 2011 Share Posted July 21, 2011 You could do a couple things: 1) You could save the output to a variable to output later. 2) Sort the array based on open / closed. 3) Create a new array with all the non-open items in this array, then output them later. Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/#findComment-1245882 Share on other sites More sharing options...
dadamssg87 Posted July 21, 2011 Author Share Posted July 21, 2011 no, i know that script that won't work. I think it needs to be some kind of loop instead of a foreach. I can't wrap my head around how to put it in a loop though. Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/#findComment-1245886 Share on other sites More sharing options...
dadamssg87 Posted July 21, 2011 Author Share Posted July 21, 2011 how would i sort the $room array by $room[$key]['available']? Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/#findComment-1245887 Share on other sites More sharing options...
teynon Posted July 21, 2011 Share Posted July 21, 2011 <?php $room = array( 0 => array('available' => 'Open', 'id' => 'First'), 1 => array('available' => 'Closed', 'id' => 'Second'), 3 => array('available' => 'Open', 'id' => 'Third'), 4 => array('available' => 'Closed', 'id' => 'Fourth')); $code=""; foreach($room as $key => $value) { if($room[$key]['available'] == "Open") { //code to display array echo "{$value['id']} - {$value['available']}<br />"; } else { //wait till all "Open" items are displayed and then display non-"open" items $code.="{$value['id']} - {$value['available']}<br />"; } } echo $code; ?> Output: First - Open Third - Open Second - Closed Fourth - Closed Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/#findComment-1245889 Share on other sites More sharing options...
dadamssg87 Posted July 21, 2011 Author Share Posted July 21, 2011 wow...didn't think about that. thanks! Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/#findComment-1245890 Share on other sites More sharing options...
AbraCadaver Posted July 21, 2011 Share Posted July 21, 2011 To sort it do this, then loop again and display: foreach($room as $key => $value) { $available[$key] = $value['available']; } array_multisort($available, SORT_DESC, $room); To output and build an array to output later: foreach($room as $value) { if($value['available'] == "Open") { $open[] = $value; } else { $closed[] = $value; } } So above you can echo the $value['whatever'] stuff in the open portion and then loop through the $closed array and echo them, or you can do this and loop again: $room = array_merge($open, $closed); And then loop over that and echo. Link to comment https://forums.phpfreaks.com/topic/242579-order-a-foreach-based-on-an-array-in-the-array/#findComment-1245897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.