0

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!

Jan
  • 1,180
  • 3
  • 23
  • 60
  • A collection of data is coming up, may be there is no index for some of the values you are checking in the record set? – Puneet May 08 '18 at 08:28
  • There is. Check the output... I dump the array just 4 lines above and the isset() methode works well... But why not in the if statement? – Jan May 08 '18 at 08:29
  • You might not be in the bit of the array you think you're in. I'd suggest doing a var_dump inside the if block, just to make sure. – GordonM May 08 '18 at 08:42
  • You have an interesting idea of how to properly handle sql results / validation. Perhaps attacking that would be a better starting point. – Mason Stedman May 08 '18 at 08:53

1 Answers1

3

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.

Dormilich
  • 927
  • 5
  • 11
  • And if it wouldn't exits I would not pass the isset() methode! – Jan May 08 '18 at 08:33
  • 1
    @Jan Please read the text carefully! As I have stated, the error occurs because you (unintentionally) use a different array than you think. And as I have also stated, the issue is NOT the `isset()`. – Dormilich May 08 '18 at 08:35
  • Gotcha... just found the error by myself.. "$isBusinessAddress = $isBusinessAddress[0]["meta_value"];" thanks! – Jan May 08 '18 at 08:37
  • 1
    The problem is that you've just used `[0]['meta_value']`, you've missed off the variable part, so here you're effectively trying to access the `meta_value` part of an array which contains a single value, `0`, which leads to your error – Jonathon May 08 '18 at 08:41