1

I have this array

Array
(
    [arr1] => Array
        (
            [0] => Array
                (                   
                    [m] => name1
                    [p] => 261.15
                    [f] => 0.30112588
                )

            [1] => Array
                (                
                    [m] => name2
                    [p] => 214.92
                    [f] => 0.35793662
                )

            [2] => Array
                (
                    [m] => name3
                    [p] => 504.35
                    [f] => 0.35779595
                )

            [3] => Array
                (
                    [m] => name3
                    [p] => 504.35
                    [f] => 0.31910875
                )

            [4] => Array
               (
                    [m] => name3
                    [p] => 504.35
                    [f] => 0.38972548
                )

            [5] => Array
                (
                    [m] => name4
                    [p] => 5500
                    [f] => 0.50674623
                )

            [6] => Array
                (
                    [m] => name5
                    [p] => 600.5
                    [f] => 0.11483321
                )
        )
)

I want to sort this array so item with higest ['p'] is on top , but i also want if some items have same ['m'] and same ['p'] than an item with lowest ['f'] is on top of these with same ['m'] and ['p'].

I managed to sort this array by ['p'] using usort function , but i cant sort it by ['f'] only if they have same ['m'] and ['p'].

Code:

$arr = array();
$arr['arr1'] = array();

array_push($arr['arr1'], ['m' => 'name1', 'p' => 261.15 ,'f' => 0.30112588]);
array_push($arr['arr1'], ['m' => 'name2', 'p' => 214.92 ,'f' => 0.35793662]);
array_push($arr['arr1'], ['m' => 'name3', 'p' => 504.35 ,'f' => 0.35779595]);
array_push($arr['arr1'], ['m' => 'name3', 'p' => 504.35 ,'f' => 0.31910875]);
array_push($arr['arr1'], ['m' => 'name3', 'p' => 504.35 ,'f' => 0.38972548]);
array_push($arr['arr1'], ['m' => 'name4', 'p' => 5500 ,'f' => 0.50674623]);
array_push($arr['arr1'], ['m' => 'name5', 'p' => 600.5 ,'f' => 0.11483321]);
3
  • 1
    Can you post your current code? Commented Sep 2, 2017 at 14:06
  • 1
    Could you please post your code, preferably with the array? It will give us an easy starting point to answer your question. Now all we have is an output of your array. Commented Sep 2, 2017 at 14:06
  • 1
    Use array_multisort - take a look at users "cagret" and "jimpoz"'s examples in the bottom of the link Commented Sep 2, 2017 at 14:15

1 Answer 1

1

With usort function:

condition list:

  • sort this array so item with higest ['p'] is on top

  • the item with lowest ['f'] is on top among the items with same ['m'] and ['p'].


// $arr is your array
usort($arr, function($a,$b){
    if ($a['m'] == $b['m'] && $a['p'] == $b['p']) {
        return ($a['f'] > $b['f'])? 1 : -1;
    }
    return ($b['p'] > $a['p'])? 1 : -1;
});

print_r($arr);

The output:

Array
(
    [0] => Array
        (
            [m] => name4
            [p] => 5500
            [f] => 0.50674623
        )

    [1] => Array
        (
            [m] => name3
            [p] => 504.35
            [f] => 0.31910875
        )

    [2] => Array
        (
            [m] => name3
            [p] => 504.35
            [f] => 0.35779595
        )

    [3] => Array
        (
            [m] => name3
            [p] => 504.35
            [f] => 0.38972548
        )

    [4] => Array
        (
            [m] => name5
            [p] => 500.5
            [f] => 0.11483321
        )

    [5] => Array
        (
            [m] => name1
            [p] => 261.15
            [f] => 0.30112588
        )

    [6] => Array
        (
            [m] => name2
            [p] => 214.92
            [f] => 0.35793662
        )
)
Sign up to request clarification or add additional context in comments.

1 Comment

Ok. I think u have to replace $arr with $arr['arr1'] in usrot function . Otherwise it look fine. I ll try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.