猿问

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

所以我有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] => 

                        )

                )


        )

没有任何工作,将不胜感激。提前致谢。


潇湘沐
浏览 129回答 1
1回答

ITMISS

您使用ksortas $result = ksort($result);,ksort返回TRUE / FALSE。这意味着您正在将其分配给$results。在这里阅读PHP ksort您的代码应为:-ksort($results);代替$result = ksort($result);您可以将其ksort用于键排序,这是一个示例$arr = [&nbsp; 5 => [1,3],&nbsp; 3 => [2,3],&nbsp; 2 => [0,7]];ksort($arr);echo '<pre>';print_r($arr);输出Array([2] => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => 0&nbsp; &nbsp; &nbsp; &nbsp; [1] => 7&nbsp; &nbsp; )[3] => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => 2&nbsp; &nbsp; &nbsp; &nbsp; [1] => 3&nbsp; &nbsp; )[5] => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => 1&nbsp; &nbsp; &nbsp; &nbsp; [1] => 3&nbsp; &nbsp; )&nbsp; )
随时随地看视频慕课网APP
我要回答