简体   繁体   中英

Why is there an undefined index on a Boolean array?

For some reasons, when I try to do an if for $value['is_set'] == false , I get undefined index error, however, if I do an isset($value['is_set']) I get 1

I did a print_r and got the 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 ) 

why is this happening?

here is my code:

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);
            } 
}

this function is called by the following code:

$so = Json::decode($request['purchase-orders']);
foreach ($so['orders'] as $key => $value) { 
        $order->create($model, $value, $datetime);
}

the full var_dump of $so is

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) }

the full error is:

 PHP Notice – yii\base\ErrorException
Undefined index: is_set

also this is related to this issue: Why does accessing array index on boolean value does not raise any kind of error?

but I don't know how to get pass this without using array...

Initially I tried empty($value['is_set']) as an alternative, but it didn't work and I freaked out. I reconsidered that option again and found that it works! I don't know why it didn't work earlier. Thanks for all your support!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM