Jump to content

Sorting multidimensional arrays


mister_O

Recommended Posts

Hi!

 

I'm kinda new to the PHP sorting rutines, and I can't seem to get it right.

 

I want to sort:

$entry[0]['month'] = 08
$entry[0]['day'] = 10
$entry[0]['dotw'] = Wed
$entry[0]['name'] = John
$entry[0]['short_text'] = "I like cats"
$entry[0]['long_text'] = "I like cats because they are cozy"

$entry[1]['month'] = 07
$entry[1]['day'] = 08
$entry[1]['dotw'] = Mon
$entry[1]['name'] = Carl 
$entry[1]['short_text'] = "Im ugly"
$entry[1]['long_text'] = "Im soo ugly that you will run away"

$entry[2]['month'] = 07
$entry[2]['day'] = 09
$entry[2]['dotw'] = Tue
$entry[2]['name'] = Peter 
$entry[2]['short_text'] = "I hate dogs"
$entry[2]['long_text'] = "I dont like dogs because they are ugly"

 

by 'month' and 'day' like:

 

$entry[0]['month'] = 07
$entry[0]['day'] = 08
$entry[0]['dotw'] = Mon
$entry[0]['name'] = Carl 
$entry[0]['short_text'] = "Im ugly"
$entry[0]['long_text'] = "Im soo ugly that you will run away"

$entry[1]['month'] = 07
$entry[1]['day'] = 09
$entry[1]['dotw'] = Tue
$entry[1]['name'] = Peter 
$entry[1]['short_text'] = "I hate dogs"
$entry[1]['long_text'] = "I dont like dogs because they are ugly"

$entry[2]['month'] = 08
$entry[2]['day'] = 10
$entry[2]['dotw'] = Wed
$entry[2]['name'] = John
$entry[2]['short_text'] = "I like cats"
$entry[2]['long_text'] = "I like cats because they are cozy"

 

I've tried using Array_multisort, but it messes up the 'dotw', 'name', 'text_short' & 'text_long' rows, probably since I dont include them in the sort...?

foreach ($entry as $key => $row) {
    $month[$key] = $row['month'];
    $day[$key] = $row['day'];
    $dotw[$key] = $row['dotw'];
    $resp[$name] = $row['name'];
    $short[$key] = $row['text_short'];
    $long[$key] = $row['text_long'];
    }
array_multisort($month, SORT_ASC, $day, SORT_ASC, $entry);

 

Which results in:

 

$entry[0]['month'] = 07
$entry[0]['day'] = 08
$entry[0]['dotw'] = Wed
$entry[0]['name'] = John
$entry[0]['short_text'] = "I like cats"
$entry[0]['long_text'] = "I like cats because they are cozy"

$entry[1]['month'] = 07
$entry[1]['day'] = 09
$entry[1]['dotw'] = Mon
$entry[1]['name'] = Carl 
$entry[1]['short_text'] = "Im ugly"
$entry[1]['long_text'] = "Im soo ugly that you will run away"

$entry[2]['month'] = 08
$entry[2]['day'] = 10
$entry[2]['dotw'] = Tue
$entry[2]['name'] = Peter 
$entry[2]['short_text'] = "I hate dogs"
$entry[2]['long_text'] = "I dont like dogs because they are ugly"

 

Anyone got a tip?

Link to comment
Share on other sites

I'd say a way to make this easier would be to have all the data in a DB then select from it and use ORDER BY

 

This would be best, especially if it is in a database already.  However, if that's not practical, try this (not tested):

 

foreach($entry as $key => $value) {
   $date[$key] = $value['month'].$value['day'];
}
array_multisort($date, SORT_ASC, $entry);

You need to build an index array $date in this case, that contains the month and day and has the same key as $entry.  Then when you multi-sort, it sorts the date array by the month and day, and then sorts the $entry array by the same key order as the sorted $date array.

Link to comment
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.