jarvis Posted January 11, 2023 Share Posted January 11, 2023 Good morning, I'm hoping someone can please kindly help. I have an array of data: $attendees[] = array( 'course'=> $course_title, 'date' => $course_start_date, 'name' => $order->get_billing_first_name().' '.$order->get_billing_last_name(), 'email' => $order->get_billing_email(), 'phone' => $order->get_billing_phone(), 'qty' => $result['quantity'], 'order' => $order_id_number ); I then group the data: // group data by course $result = array(); foreach ($attendees as $attendee): $result[$attendee['course']][] = $attendee; endforeach; Using a foreach loop (foreach ($result as $itemName => $rows):) I output the data. This works fine. However, the order isn't right. I'd like to sort the output by alphapetical. So I added the following before the foreach: sort($result); However, it doesn't make a jot of difference. Have I missed something really obvious as to why its not working? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315783-order-grouped-array/ Share on other sites More sharing options...
Barand Posted January 11, 2023 Share Posted January 11, 2023 try using a custom sort function # # Sort attendees on course, name # usort($attendees, function($a, $b) { $x = $a['course']<=>$b['course']; if ($x == 0) return $a['name']<=>$b['name']; return $x; }); # # Add sorted attendees to result array # $result = array(); foreach ($attendees as $attendee): $result[$attendee['course']][] = $attendee; endforeach; # # View result # echo '<pre>' . print_r($result, 1) . '</pre>'; P.S. You didn't say in what order you want them. If your aim is merely to get the courses in order then you could use ksort($result) (instead of your current sort() )to order the keys. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315783-order-grouped-array/#findComment-1604540 Share on other sites More sharing options...
jarvis Posted January 12, 2023 Author Share Posted January 12, 2023 Hi @Barand Many thanks (as usual!) for the reply. I added a field to the initial array called 'sort_date' => $date The $date format is dmY I've amended the usort function: usort($attendees, function($a, $b) { $x = $a['sort_date']<=>$b['sort_date']; if ($x == 0) return $a['course']<=>$b['course']; return $x; }); But sadly, the courses don't seem to be in the order of the sort date (which would be ascending). Instead, they just seem to be ignoring the sorting. Have I done something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/315783-order-grouped-array/#findComment-1604558 Share on other sites More sharing options...
Barand Posted January 12, 2023 Share Posted January 12, 2023 Dates need to be Ymd (or Y-m-d) to be sortable Quote Link to comment https://forums.phpfreaks.com/topic/315783-order-grouped-array/#findComment-1604559 Share on other sites More sharing options...
jarvis Posted January 12, 2023 Author Share Posted January 12, 2023 Ah of course! Thank you, that's sorted it Very much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/315783-order-grouped-array/#findComment-1604560 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.