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
https://forums.phpfreaks.com/topic/244395-sorting-multidimensional-arrays/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.