PHP array_multisort 缺少一些键
PHP array_multisort with some missing keys
在两个数组中我缺少键 ($billing
)。有没有一种方法可以忽略、跳过或不考虑给定的数组迭代并仍然对数组进行排序?
我收到错误消息:
array_multisort(): Argument #1 is expected to be an array or a sort flag
foreach ($table as $key => $row)
{
$billing[$key] = $row['billing']['date_due'];
}
if ($_GET['billing']=='desc') {array_multisort($billing, SORT_DESC, $table);}
else {array_multisort($billing, SORT_ASC, $table);}
您可以只检查 variable/key 是否已设置或不为空:
<?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);
}
另一种方法是在foreach
之前初始化$billing
:$billing = [];
.
在两个数组中我缺少键 ($billing
)。有没有一种方法可以忽略、跳过或不考虑给定的数组迭代并仍然对数组进行排序?
我收到错误消息:
array_multisort(): Argument #1 is expected to be an array or a sort flag
foreach ($table as $key => $row)
{
$billing[$key] = $row['billing']['date_due'];
}
if ($_GET['billing']=='desc') {array_multisort($billing, SORT_DESC, $table);}
else {array_multisort($billing, SORT_ASC, $table);}
您可以只检查 variable/key 是否已设置或不为空:
<?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);
}
另一种方法是在foreach
之前初始化$billing
:$billing = [];
.