由于某些原因,当我尝试为$value['is_set'] == false
执行if时,我得到未定义的索引错误,但是,如果我执行isset($value['is_set'])
我得到1
我做了print_r
并得到了ff:
Array ( [is_set] => [product] => Array ( [product_ID] => 1 [product_name] => Insulated Terminal Lugs 1.25 - 3Y [product_code] => [unit_ID] => 80 [unit_name] => pc(s) [price] => 50 [qty] => 1 [discount] => 0 [subtotal] => 50 [amount] => 50 ) [set] => Array ( [set_ID] => [set_items] => Array ( [0] => Array ( [amount] => 0 ) ) [amount] => 0 ) [selectedProduct] => Array ( [ID] => 1 [product_name] => Insulated Terminal Lugs 1.25 - 3Y [product_code] => [price] => 50 ) [selectedUnit] => Array ( [ID] => 80 [option_key] => pc(s) ) [amount] => 50 )
为什么会这样?
这是我的代码:
public function create($model, $value, $datetime, $transaction_type, $notes) {
if (($model->general_status == Sales::GEN_STATUS_OPEN || $model->general_status == Sales::GEN_STATUS_CLOSED) && ($model->delivery_status == Sales::DELIVERY_STATUS_DELIVERED)) {
$pth = new ProductTransactionHistory();
//var_dump($value); exit();
if ($value['is_set'] == false) {
$pth->generateSales($transaction_type, $datetime, '(add)', $model, $value, Yii::$app->user->id, $notes);
$pth->stock_in_out = ProductTransactionHistory::STOCK_OUT;
//$pth->save();
$this->saveDebug($pth);
}
}
此函数由以下代码调用:
$so = Json::decode($request['purchase-orders']);
foreach ($so['orders'] as $key => $value) {
$order->create($model, $value, $datetime);
}
$ so的完整var_dump是
array(8) { ["orders"]=> array(2) { [0]=> array(6) { ["is_set"]=> bool(false) ["product"]=> array(10) { ["product_ID"]=> string(1) "1" ["product_name"]=> string(33) "Insulated Terminal Lugs 1.25 - 3Y" ["product_code"]=> string(0) "" ["unit_ID"]=> string(2) "80" ["unit_name"]=> string(5) "pc(s)" ["price"]=> string(2) "50" ["qty"]=> int(1) ["discount"]=> int(0) ["subtotal"]=> int(50) ["amount"]=> int(50) } ["set"]=> array(3) { ["set_ID"]=> bool(false) ["set_items"]=> array(1) { [0]=> array(1) { ["amount"]=> int(0) } } ["amount"]=> int(0) } ["selectedProduct"]=> array(4) { ["ID"]=> string(1) "1" ["product_name"]=> string(33) "Insulated Terminal Lugs 1.25 - 3Y" ["product_code"]=> string(0) "" ["price"]=> string(2) "50" } ["selectedUnit"]=> array(2) { ["ID"]=> string(2) "80" ["option_key"]=> string(5) "pc(s)" } ["amount"]=> int(50) } [1]=> array(5) { ["is_set"]=> bool(true) ["product"]=> array(3) { ["unit_ID"]=> string(0) "" ["unit_name"]=> string(0) "" ["amount"]=> int(0) } ["set"]=> array(3) { ["set_ID"]=> string(1) "1" ["set_items"]=> array(2) { [0]=> array(10) { ["product_ID"]=> int(4) ["product_name"]=> string(33) "Power Miniature Solder Relay 220v" ["product_code"]=> string(5) "LY4NJ" ["unit_ID"]=> int(80) ["unit_name"]=> string(5) "pc(s)" ["qty"]=> int(1) ["price"]=> int(200) ["discount"]=> int(0) ["subtotal"]=> int(200) ["amount"]=> int(200) } [1]=> array(10) { ["product_ID"]=> int(5) ["product_name"]=> string(26) "Relay Socket 14A for LY4NJ" ["product_code"]=> string(6) "PYF14A" ["unit_ID"]=> int(80) ["unit_name"]=> string(5) "pc(s)" ["qty"]=> int(1) ["price"]=> int(20) ["discount"]=> int(0) ["subtotal"]=> int(20) ["amount"]=> int(20) } } ["amount"]=> int(220) } ["selectedSet"]=> array(3) { ["ID"]=> string(1) "1" ["set_name"]=> string(20) "LY4NJ 220v w/ Socket" ["price"]=> string(3) "220" } ["amount"]=> int(220) } } ["has_downpayment"]=> bool(false) ["payment_type"]=> string(4) "cash" ["grandTotal"]=> int(270) ["generalStatus"]=> string(6) "closed" ["statusText"]=> string(6) "Cancel" ["cssStatus"]=> string(14) "btn btn-danger" ["showPayment"]=> bool(false) }
完整的错误是:
PHP Notice – yii\base\ErrorException
Undefined index: is_set
这也与此问题有关: Why does accessing array index on boolean value does not raise any kind of error?
但我不知道如何在不使用数组的情况下传递这个...
答案 0 :(得分:0)
最初我尝试空($ value ['is_set'])作为替代方案,但它没有用,我吓坏了。我再次重新考虑这个选项,发现它有效!我不知道它为什么不提前工作。感谢您的支持!