0

I have array like this:

$items = array(
              [1] => array(
                           ['Name']   =>"Item 1", 
                           ['Prices'] => array(
                                               ['Base price'] => 80.25,
                                               ['Discount'] => 5.50
                                              )
                          ),

              [2] => array(
                           ['Name']   =>"Item 2", 
                           ['Prices'] => array(
                                               ['Base price'] => 70.25,
                                               ['Discount'] => 4.50
                                              )
                          )
              );

How can I sort $items that by "Base price"? I want to have lowest price in first element, highest in last element of output array with same structure.

Expected output:

$items = array(
               [1] => array(
                           ['Name']   =>"Item 2", 
                           ['Prices'] => array(
                                               ['Base price'] => 70.25,
                                               ['Discount'] => 4.50
                                              )
                          ),
               [2] => array(
                           ['Name']   =>"Item 1", 
                           ['Prices'] => array(
                                               ['Base price'] => 80.25,
                                               ['Discount'] => 5.50
                                              )
                          )
              );

I don't understand array_multisort() and how to use it in my case.

4
  • In your case, you should look into usort Commented Jan 20, 2014 at 13:23
  • @JanDvorak I want to sort only by ['Base price'] element. It's always there. Commented Jan 20, 2014 at 13:23
  • Why don't you try using loop.. Commented Jan 20, 2014 at 13:24
  • Better way is use ordering on DB Engine and please do not try reinvent the wheel Commented Jan 21, 2014 at 13:09

2 Answers 2

2

This is how you could use array_multisort:

foreach ($items as $item) {
    $sort[] = $item['Prices']['Base price'];
}

array_multisort($sort, SORT_ASC, $items);

Like Jan was saying, you can also use usort:

usort($items, function($a, $b) {
    return $a['Prices']['Base price'] - $b['Prices']['Base price'];
});
Sign up to request clarification or add additional context in comments.

Comments

1

I hope this helps. I'm using usort() with a callback function:

$arr = array(
    array(
        'foo' => 'bar',
        'data' => array(
            'basePrize' => 5
        )   
    ),
    array(
        'foo' => 'bar2',
        'data' => array(
            'basePrize' => 2
        )
    )
);

usort($arr, function($a, $b) {
    if($a['data']['basePrize'] === $b['data']['basePrize']) {
        return 0;
    }   

     if($a['data']['basePrize'] > $b['data']['basePrize']) {
        return 1;
    }   

    return -1
});

Comments

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.