0

I would like to know what is the appropriate method to check for true in an array key without throwing PHP notices if it doesn't exist?

My script goes through several if statements within a for each loop, and as it goes along, it creates keys in an array called "audit".

So for one iteration of the loop, the array might look like this:

$audit = 
  Array
  (
    'price_changed' => 1,
    'price_changed_to' => 10,
    'quantity_changed' => 1,
    'quantity_changed_to' => 6
  )

For the next, it could look like this:

$audit = 
  Array
  (
    'quantity_changed' => 1,
    'quantity_changed_to' => 4,
    'description_changed' => 1,
    'description_changed_to' => 'Test product'
  )

Now I want to be able to do something like this:

if($audit['price_changed']){
   .... do something ....
}

However in the case of the second item in the for each, this key doesn't exist, and my debug log fills up with PHP notices.

I'm sure I could do something like this below, but it seems like I shouldn't have to type that much for something simple like this.

if(isset($audit['price_changed'])) {
   if($audit['price_changed']) {
     .... do something ....
   }
 }

Am I thinking about this too hard or what?

Edit: this is a slimmed down example of my audit array... too many possibilities to assign zeros to all of them at the start of the loop.

2
  • 1
    Use empty function Commented Aug 17, 2016 at 19:46
  • You can combine your condition with an AND operator if you want. Commented Aug 17, 2016 at 19:49

4 Answers 4

2

Use empty() function. It will check both condtions

  • your key exists in array
  • value of the key is not false or 0 or empty string

Example:

if (!empty($audit['price_changed'])) {
    // do something
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is a great alternative also, Thank you!
2

You can use array_key_exists

if (array_key_exists('price_changed', $audit)) {
 .... do something ....
}

2 Comments

this would not evaluate if the price_changed was false
the question was about the appropriate method to check for true in an array key
0

Rather than using a nested if you can simply do:

if(isset($audit['price_changed']) && $audit['price_changed']) {
   // do something ...
}

3 Comments

So this isn't going to throw a notice even though we are still checking for something that potentially doesn't exist? Or if the isset returns false does it just give up right there automatically?
if isset returns false php wont check for the next condition because of the && operator. It will hit the do something block only when both conditions are satisfied
Ok great - I wasn't sure if It would just give up in the middle like that or not - thank you!
0

For PHP7, you can use the null coalescing operator, ??:

if ($value['price_changed'] ?? false)

if price_change isn't set, ?? will return the second arg instead, causing the if to fail, since the expression would evaluate to false:

php > $foo = array('t' => true, 'f' => false);
php > if ($foo['t'] ?? false) { echo 'true'; } else { echo 'false'; }
true
php > if ($foo['f'] ?? false) { echo 'true'; } else { echo 'false'; }
false
php > if ($foo['file_not_found'] ?? false) { echo 'true'; } else { echo 'false'; }
false

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.