-1

I want to sort a mulitdimensional array by the values position (can only be 1 or 0), the date and the time. The arrays with position = 1 should be first, and they should be sorted by date and time. The array with position = 0 should come after the ones with position = 1 and should also be sorted by date and time

Array
(
[001] => Array
    (
        [position] => 1
        [Date] => 28.04.2013
        [Time] => 00:21:38
    )

[002] => Array
    (
        [position] => 1
        [Date] => 28.04.2013
        [Time] => 00:27:07
    )

[003] => Array
    (
        [position] => 0
        [Date] => 28.04.2013
        [Time] => 00:15:06
    )

[004] => Array
    (
        [position] => 0
        [Date] => 28.04.2013
        [Time] => 00:26:09
    )

)

Thats how I want the array to be after sorting:

Array
(
[002] => Array
    (
        [position] => 1
        [Date] => 28.04.2013
        [Time] => 00:27:07
    )

[001] => Array
    (
        [position] => 1
        [Date] => 28.04.2013
        [Time] => 00:21:38
    )

[004] => Array
    (
        [position] => 0
        [Date] => 28.04.2013
        [Time] => 00:26:09
    )

[003] => Array
    (
        [position] => 0
        [Date] => 28.04.2013
        [Time] => 00:15:06
    )

)

I've tried a few functions but none of the worked right. Either the arrays with position = 1 are the last ones or all arrays just sort by date and time. I can`t figure it out by myself. Thanks in advance and sorry if my English is bad.

Kable
  • 117
  • 5
  • 13
  • 2
    There are hundreds of variations of this asked in SO, and all solutions involve the use of `usort`. Show us what you tried. – Jon Apr 27 '13 at 22:02

3 Answers3

1

Take a look at the function usort(), which takes an array and a comparison function as parameters.

Write a comparaison function that can compare two of your array elements:

  • compare position

  • if positions are equal, then compare date

  • if dates are equal, then compare time

Ben
  • 51,770
  • 36
  • 127
  • 149
Virus721
  • 8,061
  • 12
  • 67
  • 123
1

use usort() - "Sort an array by values using a user-defined comparison function"

function your_func($a, $b) {
    $pos = $b["position"] - $a["position"];
    if($pos) return $pos;

    $date = strtotime($b["Date"]) - strtotime($a["Date"]);
    if($date) return $date;

    $time = strtotime($b["Time"]) - strtotime($a["Time"]);
    return $time;
}
usort($arr, "your_func");
mychalvlcek
  • 3,956
  • 1
  • 19
  • 34
  • 1
    The arrays with position = 1 should be first. With your method you will get the 0 first when sorted. (from the doc : The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.). You should return - $pos. The same goes for the dates and times, as from the exemple given, we want a descending order. – Frédéric Clausset Apr 27 '13 at 22:20
  • u r right.. just edited! – mychalvlcek Apr 27 '13 at 22:38
1

Where the original dataset is in an array named $array...

$positions = $datetimes = array();

foreach($array as $k => $v) {

   $positions[$k] = $v['position'];
   $datetimes[$k] = strtotime($v['Date']. ' ' .$v['Time']);

}

array_multisort($positions, SORT_DESC, $datetimes, SORT_DESC, $array);

Based on comparing your data, it appears you want to sort by position DESC first, then Time (and assuming date too) DESC, so that's what this does.

Working example: http://codepad.org/exc5Dhq8

Lee
  • 10,496
  • 4
  • 37
  • 45