Search code examples
phparraysperformancephp-5.4

Check if key value pair exists in the least amount of operations an array in PHP 5.4


I want to check that the key - value pair 'purpose' => 'toggle' is not in an array of options for an element with the type checkbox.

It is possible that there is only the key 'purpose', only the value 'toggle', both as a pair, both not as a pair or neither of them.

My code looks like this:

if ($type === 'checkbox'
   && ! (array_key_exists('purpose', $options) && $options['purpose'] === 'toggle'))
{ ... }

It seems to work, but i was wondering if there is a more efficient way to do that (since those are a lot of calculations that have to be done for every element).


Solution

  • if ( $type === 'checkbox' && @$options['purpose'] != 'toggle' )  
    { ... }