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 :)

5
  • Maybe you can temporarily add those objects to the outermost level and then use multi_sort? Commented Feb 17, 2014 at 4:07
  • To get a correct answer, I'd show one or two more "entries" to show how it's nested. Commented Feb 17, 2014 at 4: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 :) Commented Feb 17, 2014 at 4:22
  • @AkshatSinghal I had in mind something like that, but I always get lost in it - can you suggest a code? Commented Feb 17, 2014 at 4: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) Commented Feb 17, 2014 at 4:50

3 Answers 3

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');
Sign up to request clarification or add additional context in comments.

2 Comments

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 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".
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.

Comments

0

Do something like the following :

$newArray = array();
foreach($MY_ARRAY as $value) {
    $newArray[] = $value[1];
}
multi_sort($newArray, $MY_ARRAY);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.