0

I have two arrays like below:

Array
(
    [27119] => Array
        (
            [firstname] => Array
                (
                    [0] => Testfname1
                )

            [lastname] => Array
                (
                    [0] => Testlname1
                )

            [photograph] => Array
                (
                    [0] => 27120
                )

            [priority] => Array
                (
                    [0] => Open
                )

            [primary_location] => Array
                (
                    [0] => 27098
                )

        )

    [27137] => Array
        (
            [firstname] => Array
                (
                    [0] => Testfname2
                )

            [lastname] => Array
                (
                    [0] => Testlname2
                )

            [photograph] => Array
                (
                    [0] => 27138
                )

            [priority] => Array
                (
                    [0] => Featured
                )

            [primary_location] => Array
                (
                    [0] => 27098
                )

        )

)

Now I want to sort the array based on the values of priority key in such a way that, if it's value is Featured then that whole element (the array containing that pair) should occur before(if there is no other candidate having 'priority => featured') the element containing 'priority => open' in the array. If it's value is Open then it's parent array should be before the parent element containing 'priority => closed'.

So to sum up, I want to sort the given multidimensional array by "priotity" value and conditionally.

I thought of using usort, but I cannot get initial idea about how to set that in those array_sort functions. Can anyone help ?

VST
  • 131
  • 1
  • 10
  • https://stackoverflow.com/a/17364128/476 → "Sorting into a manual, static order" – deceze Sep 01 '17 at 13:50
  • @deceze But this is not static data, I am gonna have many elements like above(in my question) in this array, I have given only two elements for sake of simplicity and shortness. Atleast you could have asked me in the comments first.... So the question what about multiple elements like this, how can I generalize and automate the process, without going "manual" ? – VST Sep 01 '17 at 13:54
  • As far as I understand your question, you have a decided order to sort `Featured` before `Open` before `Closed`, no? – deceze Sep 01 '17 at 13:55
  • Exactly, but there would be umpteen number of sub-arrays like I have mentioned in the question, not just two sub-arrays in main array. – VST Sep 01 '17 at 13:57
  • Then the duplicate *is correct.* Read it, understand it, it contains your solution. – deceze Sep 01 '17 at 13:57
  • But my array is multidimensional & nested and I have to work with inner sub-array, that's why the confusion, though let me try, and also in my case the key is `priority` and it's values could be `Featured` or `Open` or `Closed`, please take another look at the array in the question. – VST Sep 01 '17 at 14:00
  • `return array_search($a['priority'][0], $order) - array_search($b['priority'][0], $order)` – deceze Sep 01 '17 at 14:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153480/discussion-between-vst-and-deceze). – VST Sep 01 '17 at 14:30

1 Answers1

1

Lets assume the parent array has name testArray. Then using PHP function usort with defining comparer callback function should look like:

$sortedArray = usort($testArray, function($left, $right) {
    if($left['priority'][0] == $right['priority'][0]) { return 0; }

    if($left['priority'][0] == 'Featured' || ($left['priority'][0] == 'Opened' && $right['priority'][0] == 'Closed')) { return -1; }
    else { return 1; }
});

Try this variant

Jukka Newkampton
  • 405
  • 1
  • 6
  • 10
  • In `$sortedArray` it just returns `1`, can I get updated array in that variable? – VST Sep 01 '17 at 14:11
  • Ok got it, nevermind, this will work generalisedly for any number of subarrays, right ? – VST Sep 01 '17 at 14:14
  • But here keys are not maintained how can I maintain key (main key) of subarrays respectively (eg. [27119]) ? – VST Sep 01 '17 at 15:11
  • May be usort works on reference, so you don't need to assign its result to any variable. If you want to sort with respect for keys, you can use uasort. If you need to do any maintaining by keys, you can use array_walk function, which callback receives key-value pair – Jukka Newkampton Sep 02 '17 at 11:17
  • Ok thanks for the explanation, accepted your answer, you can upvote the question if you feel this was worth asking – VST Sep 02 '17 at 13:15