使用数字键对多维数组排序,但保持键不变,只是更改顺序

时间:2019-04-27 05:41:16

标签: php arrays sorting multidimensional-array

所以我有3维数组。我希望根据键对数组进行重新排序,但键的值应保持不变。例如,如果数组键为5,2,4,1,3,则它应变为1,2,3,4,5。在下面,我提供了我拥有的阵列和例外阵列以及我尝试过的解决方案。

这是我拥有的数组:-

[5] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => E3
                            [deal_text] => 
                            [units] => 5
                            [total_units] => 5
                            [amount] => 2620.8333333333
                            [is_freezed] => 
                            [can_sell] => 1
                        )

                )

        )
[2] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => E4
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 516.66666666667
                            [is_freezed] => 1
                            [can_sell] => 
                        )

                )

        )
[4] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => C8
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 526.66666666667
                            [is_freezed] => 1
                            [can_sell] => 
                        )
                )

        )
[1] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => D4
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 557.14285714286
                            [is_freezed] => 1
                            [can_sell] => 
                        )
                )
        )
[3] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => E5
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 516.66666666667
                            [is_freezed] => 1
                            [can_sell] => 
                        )

                )

        )

以下是我尝试过的解决方案:-

$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));

这是预期的数组:-

Array
(
    [1] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => D4
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 557.14285714286
                            [is_freezed] => 1
                            [can_sell] => 
                        )
                )
        )

    [2] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => E4
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 516.66666666667
                            [is_freezed] => 1
                            [can_sell] => 
                        )

                )

        )

    [3] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => E5
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 516.66666666667
                            [is_freezed] => 1
                            [can_sell] => 
                        )

                )

        )

    [4] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => C8
                            [deal_text] => 
                            [units] => 1
                            [total_units] => 0
                            [amount] => 526.66666666667
                            [is_freezed] => 1
                            [can_sell] => 
                        )
                )

        )

    [5] => Array
        (
            [Anfield] => Array
                (
                    [0] => Array
                        (
                            [slot] => E3
                            [deal_text] => 
                            [units] => 5
                            [total_units] => 5
                            [amount] => 2620.8333333333
                            [is_freezed] => 
                            [can_sell] => 1
                        )

                )

        )

)

没有任何效果,将不胜感激。预先感谢。

1 个答案:

答案 0 :(得分:2)

  

您将ksort用作$result = ksort($result);ksort返回TRUE / FALSE。这意味着您正在将其分配给$results。   在此处阅读PHP ksort

您的代码应为:-

ksort($results);

代替

$result = ksort($result);

您可以使用ksort进行键排序,这是一个示例

$arr = [
  5 => [1,3],
  3 => [2,3],
  2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);

输出

Array
(
[2] => Array
    (
        [0] => 0
        [1] => 7
    )

[3] => Array
    (
        [0] => 2
        [1] => 3
    )

[5] => Array
    (
        [0] => 1
        [1] => 3
    )

  )