gpt4 book ai didi

PHP array_multisort 没有正确排序第二个数组

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:19 25 4
gpt4 key购买 nike

这是我的代码:

$numbers = array(10, 100, 100, 0);
$names = array("Alex", "Jane", "Amanda", "Debra");
array_multisort($numbers, $names);

print_r($numbers);
print_r($names);

以上代码输出:

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

Array
(
[0] => Debra
[1] => Alex
[2] => Amanda
[3] => Jane
)

为什么第二个数组的排序不正确?如果它是正确的,谁能解释它是如何正确的?

谢谢。

最佳答案

是的,这是正确的。您以错误的方式使用了 PHP 的“array_multisort”函数。它不会对两个数组进行排序,但会对第一个数组进行排序,第二个数组将按照与第一个数组相对应的顺序进行定位。

$numbers 排序前:

(
[0] => 10
[1] => 100
[2] => 100
[3] => 0
)

排序后:

(
[0] => 0 (position before sorting - 3rd)
[1] => 10 (position before sorting - 0)
[2] => 100 (position before sorting - 2 or 1)
[3] => 100 (position before sorting - 2 or 1)
)

因此,第二个数组不会排序,但会根据第一个数组定位其元素。

(
[0] => Debra --in first array 3rd element has moved to 0th position
[1] => Alex -- in first array 0th element has moved to 1st position
[2] => Amanda -- in first array 2nd element has moved to 2nd position
[3] => Jane -- in first array 1st element has moved to 3rd position
)

关于PHP array_multisort 没有正确排序第二个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45272616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com