phparrayslaravelissetundefined-index

Isset returns undefined index although it is set


I have a weird problem. I call a self written function which returns my SQL result.

$isBusinessAddress = PartnernetShop::invoiceAddressIsBusinessAddress($userID)->toArray();

I want to get the meta_value of the first array cause this is the returned value I need. For security reasons I check if the index and value exits:

if(isset($isBusinessAddress[0]["meta_value"])){ ... }

This if statement returns:

Undefined index: meta_value

However, if I check the value just 2-3 lines above it works!

var_dump($isBusinessAddress);
var_dump(isset($isBusinessAddress[0]["meta_value"]));
var_dump(array_key_exists("meta_value", $isBusinessAddress[0]));
var_dump($isBusinessAddress[0]["meta_value"]);

Output:

array(1) {
  [0]=>
  array(2) {
    ["user_id"]=>
    int(12)
    ["meta_value"]=>
    string(4) "Nein"
  }
}
bool(true)
bool(true)
string(4) "Nein"

This is my code:

$isBusinessAddress = PartnernetShop::invoiceAddressIsBusinessAddress($userID)->toArray();

var_dump($isBusinessAddress);
var_dump(isset($isBusinessAddress[0]["meta_value"]));
var_dump(array_key_exists("meta_value", $isBusinessAddress[0]));
var_dump($isBusinessAddress[0]["meta_value"]);
//die();

if(isset($isBusinessAddress[0]["meta_value"])){
    $isBusinessAddress = [0]["meta_value"];
}else{
    $isBusinessAddress = "NotSet";
}

Can you guys tell me what I am doing wrong? I have used this if statement already multiple times...

UPDATE: The index definitely exists cause I pass the if statement. I just get message "Undefined index: meta_value" on the line trying to assign the value to the variable! Following line:

$isBusinessAddress = [0]["meta_value"];

Kind regards and Thank You!


Solution

  • The problem is not the isset(), but the line afterwards:

    $isBusinessAddress = [0]["meta_value"];
    

    which tries to get the key meta_value from the array [0] (resp. array(0 => 0)) where this key does not exist.