如何在PHP中基于索引数组对二维数组进行排序?

时间:2017-12-09 16:16:02

标签: php arrays sorting multidimensional-array

我在PHP中有一个像这样的数组:

阵 (     [0] =>排列         (             [0] =>黑色             [2] =>棕色             [1] =>红色         )

[1] => Array
    (
        [2] => car
        [0] => bicycle
        [1] => motorcycle
    )

如何获取阵列,如下所示:

结果:

阵 (     [0] =>排列         (             [0] =>黑色             [1] =>红色             [2] =>棕色         )

[1] => Array
    (

        [0] => bicycle
        [1] => motorcycle
        [2] => car
    )

由于

2 个答案:

答案 0 :(得分:0)

使用krsort()函数根据键以降序对关联数组进行排序。

答案 1 :(得分:0)

你可以尝试这样:

$arr=array(
    array(2=>'red',1=>'green',3=>'black'),
    array(1=>'pink',3=>'blue',2=>'yellow')
);
array_walk( $arr, function(&$v,$k){
    ksort($v);
});
echo '<pre>', print_r( $arr, true ), '</pre>';

输出:

Array
(
    [0] => Array
        (
            [1] => green
            [2] => red
            [3] => black
        )

    [1] => Array
        (
            [1] => pink
            [2] => yellow
            [3] => blue
        )

)