php 使用array_search进行多值搜索

xeufq47z  于 2023-06-20  发布在  PHP
关注(0)|答案(4)|浏览(114)
$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]);

这很好用,但是有没有一种方法可以使用多个值进行搜索:例如:获取name='flash' && type='camera'的关键点在哪里?

x8diyxa7

x8diyxa71#

有没有一种方法可以使用多个值进行搜索:例如,get key where name='flash' && type='camera'?
array_keys函数:

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

输出:

Array
(
    [0] => 3
)
kcrjzv8t

kcrjzv8t2#

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

注意:当前的搜索将无法正常运行,它将始终返回零索引,因为array_search返回0或false。

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

lymgl2op3#

我想我会用一个循环来创建我自己的函数,它只会根据一个或两个参数来检索我想要的数组。

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'));

为您提供:

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

反向匹配示例(type而不是name):

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

为您提供:

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

bfhwhh0e4#

RomanPerekhrest & RBA实现答案

<?php

function arraySearchMultiKeyVal($dataList = [], $arrKeyValFind = [], $is_return_key = false) {
    // Implement from answer : https://stackoverflow.com/q/45124763

    $index = array_keys($dataList, $arrKeyValFind);

    if ($index) {
        $key = $index[0];
        if ($is_return_key) {
            return $key;
        } else {

            return $dataList[$key];
        }
    }

    return false;
}

$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'
    )
);

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

会回来的

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

相关问题