排序函数是否考虑多级数组中的叶节点?
下面的这段代码旨在创建一个多级数组,打印它,然后打乱它,再次打印它,并对数组进行排序。
$arr=array(
array(
array('a','b','c')
),
array(
array('d','e','f')
),
array(
array('g','h','i')
),
);
print_r($arr);
shuffle($arr);
print_r($arr);
sort($arr);
print_r($arr);
现在我观察到奇怪的事情,当使用 shuffle()
时,它只对正在洗牌的数组的索引进行洗牌,而不对最里面的元素 a,b, c
为其他内容,但是当使用 sort()
函数时,它将数组排序回正常状态,并且叶节点返回到字母顺序强>。为什么会出现这种情况?
这是示例输出: *原始数组*
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)
打乱数组
Array
(
[0] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
[1] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[2] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
)
排序数组
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)
This following piece of code is meant to create a multilevel array, print it, then shuffle it, again print it, and sort the array.
$arr=array(
array(
array('a','b','c')
),
array(
array('d','e','f')
),
array(
array('g','h','i')
),
);
print_r($arr);
shuffle($arr);
print_r($arr);
sort($arr);
print_r($arr);
Now the odd thing that i observed, when shuffle()
is used, it only shuffles the indexes of the array that is being shuffled, it doesnt shuffle, the inner most elements a,b,c
to something else, but when the the sort()
function is used, it sorts back the array into the normal status and the leaf nodes are back to alphabetical order. Why does this happen?
Here is the sample output:
*Original Array*
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)
Shuffled Array
Array
(
[0] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
[1] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[2] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
)
Sorted Array
Array
(
[0] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[1] => Array
(
[0] => Array
(
[0] => d
[1] => e
[2] => f
)
)
[2] => Array
(
[0] => Array
(
[0] => g
[1] => h
[2] => i
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 PHP 的比较运算符参考的数组部分,尤其是数组比较转录。基本上,PHP 首先比较数组中键的数量,然后检查数组是否具有相同的键(在本例中,内部数组为
0
),然后比较值。因为那里有一个嵌套数组,所以它会继续比较sort()
中的叶节点,这会导致在这种情况下按叶数组的第一个值(a
、d
和g
)。shuffle()
只是对您提供的数组的索引进行重新排序,无论数组包含什么,因此它根本不会触及内部数组。See the array section of PHP's comparison operator reference, especially the array comparison transcription. Basically PHP first compares the amount of keys in the array, then checks if the arrays have the same keys (in this case,
0
for the inner arrays) and then compares the values. Because you have a nested array there, it proceeds to compare the leaf nodes insort()
, which leads to the array being sorted in this case by the first value of the leaf arrays (a
,d
andg
).shuffle()
simply reorders the indexes of the array you give it regardless of what the array contains, thus it does not touch the inner arrays at all.