排序函数是否考虑多级数组中的叶节点?

发布于 2024-11-14 05:05:54 字数 2542 浏览 1 评论 0原文

下面的这段代码旨在创建一个多级数组,打印它,然后打乱它,再次打印它,并对数组进行排序。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2024-11-21 05:05:54

请参阅 PHP 的比较运算符参考的数组部分,尤其是数组比较转录。基本上,PHP 首先比较数组中键的数量,然后检查数组是否具有相同的键(在本例中,内部数组为 0),然后比较值。因为那里有一个嵌套数组,所以它会继续比较 sort() 中的叶节点,这会导致在这种情况下按叶数组的第一个值(adg)。

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 in sort(), which leads to the array being sorted in this case by the first value of the leaf arrays (a, d and g).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文