用条件对数组进行排序

伏特

如果[stock]的值= 0,我需要按[price]的值对数组(下方)进行排序,我也需要对它们进行排序,但它们的位置应比[stock]> 0的位置低。对于任何数量的子数组,较低的数组仅是示例。

我有阵列

Array
(
[0] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 0
        [price] => 5
        [shop] => cernyrytir.cz
    )

[1] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 20
        [price] => 9
        [shop] => mysticshop.cz
    )

[2] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 5
        [price] => 5
        [shop] => najada.cz
    )

[3] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 0
        [price] => 3
        [shop] => rishada.cz
    )
)

我需要数组:

Array
(
[2] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 5
        [price] => 5
        [shop] => najada.cz
    )

[1] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 20
        [price] => 9
        [shop] => mysticshop.cz
    )

[3] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 0
        [price] => 3
        [shop] => rishada.cz
    )

[0] => Array
    (
        [cardname] => Tundra Wolves
        [edition] => Legends
        [stock] => 0
        [price] => 5
        [shop] => cernyrytir.cz
    )
)

在此先感谢您的帮助。

拉斐尔·菲盖雷多(Rafael Figueiredo)

好的,这就是您想要的:

foreach($a as $key => $value) {
    if ($value['stock'] > 0) {
        $stock[] = $value;
        $stockPrice[] = $value['price'];
    } else {
        $zeroStock[] = $value;
        $zeroStockPrice[] = $value['price'];
    }
}

array_multisort($stockPrice, SORT_ASC, $stock);
array_multisort($zeroStockPrice, SORT_ASC, $zeroStock);

$array = array_merge($stock, $zeroStock);

现在,$ array具有您想要的功能。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章