按两个标准对多维数组进行排序



我有一个多维数组,其结构如下:

$futureInvoices[] = Array(
    "date" => date("Y-m-d", $ts),
    "customer" => $possibleDomain->userid,
    "subtotal" => "0.00",
    "total" => "0.00",
);

我只想对数组$futureInvoices中的条目按日期升序排序,然后按数量降序排序。这怎么可能呢?

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);
?>
In this example, we will order by volume descending, edition ascending.
We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
在您的示例中,

循环将提取date和total的值。那么对函数的调用将是相同的

相关内容

  • 没有找到相关文章

最新更新