简体   繁体   中英

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' ?

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
)

The array_search function accepts an array as parameters the following will work for the use case you provided.

$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'
  )
);
$compare = array(
    'name'=>'flash',
    'type'=>'camera'
);
$key = array_search($compare, $haystack);
var_dump($haystack[$key]);

Note: your current search will not function correctly it will always return the zero index because the array_search returns 0 or false.

$key = array_search('flash', array_column($array_subjected_to_search, 'name'));
var_dump($array_subjected_to_search[$key]);

I think I would just make my own function using a loop that will just retrieve the array I want based on either one or two parameters.

function getValueMatch($array, $val1, $val2 = false, $type = 'name')
    {
        foreach($array as $key => $row) {
            # See note below, but it might be handy to have a reversible key name
            if($row[$type] == $val1) {
                if($val2) {
                    # You can put a changeable key name to reverse-find
                    # It might be helpful to search for the other key first
                    # at some point, best to keep your options open!
                    $altVar = ($type == 'name')? 'type' : 'name';
                    if($row[$altVar] == $val2)
                        return $row;
                }
                else
                    return $row;
            }
        }
    }

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

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

  array(
          'name' => 'snart',
          'type' => 'antihero'
      ),
  array(
        'name' => 'flash',
        'type' => 'camera'
      )
  );

print_r(getValueMatch($array,'flash','camera'));

Gives you:

Array
(
    [name] => flash
    [type] => camera
)

Example of reverse match ( type instead of name ):

print_r(getValueMatch($array,'antihero',false,'type'));

Gives you:

Array
(
    [name] => snart
    [type] => antihero
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM