PHP:具有多重排序的多维数组



我有一个包含4个值的数组

$array[0] = array('amount' => '98.60', 'typeA' => '98.52', 'typeB' => '58.52', 'typeC' => '90.2');
$array[1] = array('amount' => '55.80', 'typeA' => '25.36', 'typeB' => '36.54', 'typeC' => '36.99');
$array[2] = array('amount' => '42.68', 'typeA' => '64.26', 'typeB' => '65.87', 'typeC' => '99.24');
$array[3] = array('amount' => '812.3', 'typeA' => '36.27', 'typeB' => '23.25', 'typeC' => '94.35');

我需要根据每个键的最大值排列数组,序列为:

  1. typeA
  2. typeB
  3. typeC

所以最后我将看到哪个是最上面的。

希望这里能给一些帮助,谢谢!

$ data =数组(数组("量"=>"98.60","typeA ' => ' 98.52 ', ' typeB ' => ' 58.52 ', ' typeC ' => ' 90.2 '),数组('多' => ' 55.80 ',' typeA ' => ' 25.36 ', ' typeB ' => ' 36.54 ', ' typeC ' => ' 36.99 '),数组('多' => ' 42.68 ',' typeA ' => ' 64.26 ', ' typeB ' => ' 65.87 ', ' typeC ' => ' 99.24 '),数组('多' => ' 812.3 ',' typeA ' => ' 36.27 ', ' typeB ' => ' 23.25 ', ' typeC ' => ' 94.35 '));

//获取列列表

foreach ($data as $key => $row) {
$volume[$key]  = $row['typeA'];
$edition[$key] = $row['typeB'];
$edition1[$key] = $row['typeC'];

}

array_multisort($volume, SORT_DESC, $edition, SORT_DESC, $edition1, SORT_DESC, $data);

回声";print_r(元数据),

array_multisort()可用于一次对多个数组进行排序,也可用于对多维数组按一个或多个维度进行排序。

http://php.net/manual/en/function.array-multisort.php

<?php
    $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);
    // Pass the array, followed by the column names and sort flags
    $sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);
?>

下面是另一个使用它的好例子:

PHP按两个字段值排序数组

相关内容

  • 没有找到相关文章