Heim  >  Artikel  >  Backend-Entwicklung  >  深入array multisort排序原理的详解_PHP教程

深入array multisort排序原理的详解_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:05:26862Durchsuche

复制代码 代码如下:

$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?>

复制代码 代码如下:

$a = array(1,2,3);
$b = array(3);
// 将数据根据 volume 降序排列,根据 edition 升序排列
// 把 $data 作为最后一个参数,以通用键排序
array_multisort($a, $b, $data);//数组一维个数不同
var_dump( $data);
?>

上面这个测试代码提示数组不一致的个数
Warning: array_multisort(): Array sizes are inconsistent in G:\www\test\index.php on line 15

再看
复制代码 代码如下:

$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7, 3, 4,4);//二维个数不同
?>

复制代码 代码如下:

$a = array(11,2,3,4,5,6);
$b = array(3,3,3,3,3,3);
//从结果中看到对应11的array(67,7,3,4,4)按相同顺序出现了;
// 将数据根据 volume 降序排列,根据 edition 升序排列
// 把 $data 作为最后一个参数,以通用键排序
array_multisort($a, $b, $data);
var_dump( $a,$data);
?>

从上面的结果可以得知:
数组参数必须有相同的一维个数;
然后每个数组的对应位置(注意不是相同key,而是从自然位置对应着,如$a(1=>4),对应$b(99=>4) 的4,因为它们的位置都是第一个,而非key(1,99)的对应关系),对应位置中的值就像穿在一个个互相平等竹杆上一样,穿在上面的其中一个值需要调整位置时,就会导致同一"竹杆"上的其它值出现垂直移动.
 
类:
$a           $b          $c
4=>7    8=>10       '999' => 0
9=>9   0=>1       999=>9
0=> 2   9=> 3    9999=>7
----------------------
如果出现按$a的7与9对换,也就会带动$b的10与1对换 $c的0与9对换.
所以关系就像上面的三个数组,同色的在同一"阵线上",其中一个换,大家要一起换位置.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327698.htmlTechArticle复制代码 代码如下: ?php $data[] = array('volume' = 67, 'edition' = 2); $data[] = array('volume' = 86, 'edition' = 1); $data[] = array('volume' = 85, 'edition' = 6); $data[]...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn