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

4

2 回答 2

2

您可以像这样使用 PHP 函数usort

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是一个 PHP 函数,允许您根据需要对数组进行排序。创建一个函数,如果它们相同,则返回 0,如果在 $b 之前需要 $a,则返回 -1,如果在 $b 之后需要 $a,则返回 1。

于 2013-07-05T12:49:07.423 回答
0

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

于 2013-07-05T12:46:42.633 回答