这是我的示例数组$ postcodeSuppliers:
Array
(
[0] => AB123
[postcode] => AB123
[1] => TEST
[supplier_1] => TEST
[2] =>
[supplier_2] =>
[3] =>
[supplier_3] =>
)
我一直在努力确认供应商是否为空。 这是我一直用于此的代码:
function generateQuoteSuppliers($postcodeSuppliers) {
$quoteSupplier = array("supplier_1", "supplier_2", "supplier_3");
//print("<pre>");
//print_r($postcodeSuppliers);
//print("</pre>");
for ($i = 1; $i < 4; $i++) {
$supplier = $postcodeSuppliers['supplier_' . $i . ''];
//if ($supplier == '')
//if (!isset($supplier))
if (empty($supplier)) {
//A fake supplier is added here 'FAKE' if any of the 3 suppliers contain no date
echo "NO SUPPLIER";
$quoteSupplier['supplier_' . $i . ''] = array
(
'supplier' => 'FAKE',
'price' => 0
);
} else {
$quoteSupplier['supplier_' . $i . ''] = array
(
'supplier' => $postcodeSuppliers['supplier_' . $i . ''],
'price' => 0
);
}
}
return $quoteSupplier;
}
我用来检查值是否为空的方法都没有。我明白了:
Array
(
[0] => supplier_1
[1] => supplier_2
[2] => supplier_3
[supplier_1] => Array
(
[supplier] => TEST
[price] => 0
)
[supplier_2] => Array
(
[supplier] =>
[price] => 0
)
[supplier_3] => Array
(
[supplier] =>
[price] => 0
)
)
当我期待这个时:
Array
(
[0] => supplier_1
[1] => supplier_2
[2] => supplier_3
[supplier_1] => Array
(
[supplier] => TEST
[price] => 0
)
[supplier_2] => Array
(
[supplier] => FAKE
[price] => 0
)
[supplier_3] => Array
(
[supplier] => FAKE
[price] => 0
)
)
有人能告诉我出错的地方吗?我完全乐于接受我错的事实!或者错误地使用我的数组。
答案 0 :(得分:3)
您可以使用语言构造函数isset()
答案 1 :(得分:1)
在将trim()
传递给$supplier
empty()