PHP array_multisort包含一些缺少的键

时间:2016-04-27 03:06:21

标签: php array-multisort

在两个数组中,我错过了键(Recipe.by_ingredient(apple).by_ingredient(orange))。有没有办法可以忽略,跳过或者只是不考虑给定的数组迭代并仍然对数组进行排序?

我收到错误消息:

  

array_multisort():参数#1应该是一个数组或一个排序标志

$billing

1 个答案:

答案 0 :(得分:1)

您可以检查变量/键是否设置为空:

<?php
foreach ($table as $key => $row) {
   $billing[$key]  = isset($row['billing']['date_due']) ? $row['billing']['date_due'] : null;
}

// $billing doesn't exist, if $table is empty
if (!empty($billing)) {
  array_multisort($billing, ($_GET['billing']=='desc' ? SORT_DESC : SORT_ASC), $table);
} else {
  array_multisort($table); // or simply sort($table);
}

另一种方法是在$billing之前初始化foreach$billing = [];

相关问题