Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/315783-order-grouped-array/
Share on other sites

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.

  • Great Answer 1
Link to comment
https://forums.phpfreaks.com/topic/315783-order-grouped-array/#findComment-1604540
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/315783-order-grouped-array/#findComment-1604558
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.