douzhuolong9886
2011-02-25 17:46
浏览 46
已采纳

PHP array_multisort手动示例

Why is 4 the first value in $ar2? I am pretty sure 4 should be at the end of the sorted array since its naturally sorting as SORT_ASC.

<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

array(4) {
  [0]=> int(0)
  [1]=> int(10)
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(4)
  [1]=> int(1)
  [2]=> int(2)
  [3]=> int(3)
}

图片转代码服务由CSDN问答提供 功能建议

为什么4是$ ar2中的第一个值? 我很确定4应该在排序数组的末尾,因为它自然地排序为SORT_ASC。

 &lt;?php 
 $ ar1 = array(10,100,  100,0); 
 $ ar2 = array(1,3,2,4); 
array_multisort($ ar1,$ ar2); 
 
var_dump($ ar1); 
var_dump($ ar2); 
?  &gt; 
   
 
 

在此示例中,排序后,第一个数组将包含0,10,100,100。第二个数组将包含4 对第一阵列(100和100)中的相同条目对应的第二阵列中的条目也进行了排序。

  array(4){
 [0] =&gt;  int(0)
 [1] =&gt;  int(10)
 [2] =&gt;  int(100)
 [3] =&gt;  int(100)
} 
array(4){
 [0] =&gt;  int(4)
 [1] =&gt;  int(1)
 [2] =&gt;  int(2)
 [3] =&gt;  int(3)
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douba8048 2011-02-25 17:51
    已采纳

    You already copied it form the manual:

    In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

    That means the second array is sorted by the order of the first array (the first array defines the order for the second array)

    4 in $ar2 corresponds to 0 in $ar1. 0 is at the first position in $a1, hence 4 is the first in $ar2.

    点赞 打赏 评论

相关推荐 更多相似问题