Skip to content
Advertisement

Using array_search for multi value search

  $array_subjected_to_search =array(
  array(
          'name' => 'flash',
          'type' => 'hero'
      ),

  array(
          'name' => 'zoom',
          'type' => 'villian'
      ),

  array(
          'name' => 'snart',
          'type' => 'antihero'
      ),
  array(
        'name' => 'flash',
        'type' => 'camera'
      )
  );
  $key = array_search('flash', array_column($array_subjected_to_search, 'name'));
  var_dump($array_subjected_to_search[$key]);

This works fine, but is there a way to search using multiple values: eg. get key where name='flash' && type='camera' ?

Advertisement

Answer

is there a way to search using multiple values: eg. get key where name=’flash’ && type=’camera’ ?

Simply with array_keys function:

$result_key = array_keys($array_subjected_to_search, [ 'type' => 'camera','name' => 'flash']);
print_r($result_key);

The output:

Array
(
    [0] => 3
)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement