5

My array:

$MY_ARRAY = 
Array
(
    [0] => Array
        (
            [0] => 2861
            [1] => Array
                (
                    [start_month] => 6
                    [start_year] => 1970
                    [end_month] => 12
                    [end_year] => 1990
                    [experience_info] => "Practically a random string"
                )

        )

)

And I would like to sort $MY_ARRAY direct children by their inner contents, ideally in an order of start_year, start_month, end_year, end_month. I guess I could use the array_multisort() somehow, but I don't know how. Does anyone know how to deal with this?

Thanks.

EDIT: As it showed up, the solution was nice and simple, what I didnt know is that during the comparsion in callback-compare-function you can go to the deeper structure - so if your deeper than lvl-1 indexes remains always the same (my case) that is how to do it :)

jave.web
  • 13,880
  • 12
  • 91
  • 125
  • Maybe you can temporarily add those objects to the outermost level and then use `multi_sort`? – Akshat Singhal Feb 17 '14 at 04:07
  • To get a correct answer, I'd show one or two more "entries" to show how it's nested. – Jacob Budin Feb 17 '14 at 04:07
  • @JacobBudin just the "level 1" index is changing, any deeper indexes remain the same. But I thought that was clear enaugh from my description :) – jave.web Feb 17 '14 at 04:22
  • @AkshatSinghal I had in mind something like that, but I always get lost in it - can you suggest a code? – jave.web Feb 17 '14 at 04:28
  • @jave.web Create another array with the inner values, say $newArray, using `$newArray[0] = $MY_ARRAY[0][1]` and then do `multi_sort($newArray,$MY_ARRAY)` – Akshat Singhal Feb 17 '14 at 04:50

3 Answers3

1

For this purpose you can use uasort function:

function compare_callback($arr1, $arr2) {
    $start_year1 = $arr1[1]['start_year'];
    $start_year2 = $arr2[1]['start_year'];

    $start_month1 = $arr1[1]['start_month'];
    $start_month2 = $arr2[1]['start_month'];

    $end_year1 = $arr1[1]['end_year'];
    $end_year2 = $arr2[1]['end_year'];

    $end_month1 = $arr1[1]['end_month'];
    $end_month2 = $arr2[1]['end_month'];

    return ($start_year1 === $start_year2)
        ? (($start_month1 === $start_month2)
            ? (($end_year1 === $end_year2)
                ? (($end_month1 === $end_month2)
                    ? 0
                    : (($end_month1 < $end_month2) ? -1 : 1))
                : (($end_year1 < $end_year2) ? -1 : 1))
            : ($start_month1 < $start_month2) ? -1 : 1)
        : (($start_year1 < $start_year2) ? -1 : 1);
}

uasort($array, 'compare_callback');
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
  • Nice ternary, and in a spirit of accepting the answer that fully naswers the question - accepted, and for the fact that this shows the "ORDER BY" thingie :) – jave.web Feb 17 '14 at 06:15
  • @jave.web Nice question :) Actually this is not duplicate. I think you need to edit question header to be more clear. Something like this: "Sort multiarray with given field order". – Alexander Yancharuk Feb 17 '14 at 06:48
1

You can use PHP's usort function and supply your own comparison function. Like this:

function cmp($a, $b)
{
    if ($a[1]['start_year'] == $b[1]['start_year'])
    {
        // You can further do tests for start_month, etc in here if start_years are equal
        return 0;
    }
    return ($a[1]['start_year'] > $b[1]['start_year']) ? 1 : -1;
}

usort($MY_ARRAY, "cmp");

The above will sort your array by start_year. I haven't tested the code, but it should work.

Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
0

Do something like the following :

$newArray = array();
foreach($MY_ARRAY as $value) {
    $newArray[] = $value[1];
}
multi_sort($newArray, $MY_ARRAY);
Akshat Singhal
  • 1,801
  • 19
  • 20