1

i need to sort an array like this in function of the price:

Array
(
    [SJ] => Array
        (
            [desc] => Junior Suite
            [solutions] => Array
                (
                    [0] => Array
                        (
                            [code] => BB
                            [desc] => Bed and Breakfast
                            [price] => 607.08
                            [status] => OK
                            [policy] => 1
                            [currency] => EU
                        )
                    [1] => Array
                        (
                            [code] => BB
                            [desc] => Bed and Breakfast
                            [price] => 700.80
                            [status] => OK
                            [policy] => 1
                            [currency] => EU
                        )

                )

        )

    [MZ] => Array
        (
            [desc] => Doble Deluxe con hidromasaje
            [solutions] => Array
                (
                    [0] => Array
                        (
                            [code] => BB
                            [desc] => Bed and Breakfast
                            [price] => 518.40
                            [status] => OK
                            [policy] => 1
                            [currency] => EU
                        )

                )

        )

)

Can someone give me the right way to do that? :)

EXPECTED RESULT

Array
(
    [MZ] => Array
        (
            [desc] => Doble Deluxe con hidromasaje
            [solutions] => Array
                (
                    [0] => Array
                        (
                            [code] => BB
                            [desc] => Bed and Breakfast
                            [price] => 518.40
                            [status] => OK
                            [policy] => 1
                            [currency] => EU
                        )

                )

        )
    [SJ] => Array
        (
            [desc] => Junior Suite
            [solutions] => Array
                (
                    [0] => Array
                        (
                            [code] => BB
                            [desc] => Bed and Breakfast
                            [price] => 607.08
                            [status] => OK
                            [policy] => 1
                            [currency] => EU
                        )
                    [1] => Array
                        (
                            [code] => BB
                            [desc] => Bed and Breakfast
                            [price] => 700.80
                            [status] => OK
                            [policy] => 1
                            [currency] => EU
                        )

                )

        )
)

What i need is to order that array in function of price. So if i have many solutions, i need to take the one that have minor price

1
  • 2
    1) What have you tried ? 2) What's the expected output ? Commented Jul 5, 2013 at 12:41

2 Answers 2

2

You can use the PHP function usort like this:

function sortInnerPrice($a, $b) {
    if ($a['price'] == $b['price']) {
        return 0;
    }

    return ($a['price'] < $b['price']) ? -1 : 1;
}

// First sort the inner prices
foreach ($test as $key => $val) {
    usort($test[$key]['solutions'], 'sortInnerPrice');
}

function cmp($a, $b)
{
    $aPrice = $a['solutions'][0]['price'];
    $bPrice = $b['solutions'][0]['price'];
    if ($aPrice == $bPrice) {
        return 0;
    }
    return ($aPrice < $bPrice) ? -1 : 1;
}

// Then sort by lowest solution price
usort($yourArray, "cmp");

usort is a PHP function that allows you to sort an array based on whatever you want. You create a function that will return 0 if they are the same, -1 if you want $a before $b, and 1 if you want $a after $b.

Sign up to request clarification or add additional context in comments.

3 Comments

sorry, that was my mistake.. i have many solutions.. in that example there is only one.. but generally there are many solutions..
how do you want it sorted. By the average of solutions? the lowest solution? highest solution?
@JackTurky You'll probably have to first sort the solution prices separately then sort the outer objects by the lowest solution
0

Try to make another array containing all the prices and then use array_multisort for it

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.