Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:Search多维数组=>;获取数组\u键_Php_Arrays_Multidimensional Array_Associative Array - Fatal编程技术网

PHP:Search多维数组=>;获取数组\u键

PHP:Search多维数组=>;获取数组\u键,php,arrays,multidimensional-array,associative-array,Php,Arrays,Multidimensional Array,Associative Array,通过在第二级数组中搜索值来获取数组键的最佳/最快方法是什么 $test = array( 'name_01' => array('yellow', 'red', 'blue', 'black', 'white', 'purple'), 'name_02' => array('red', 'blue', 'white', 'green'), 'name_03' => array('blue', 'pink', 'purple', 'blue'),

通过在第二级数组中搜索值来获取数组键的最佳/最快方法是什么

$test = array(
    'name_01' => array('yellow', 'red', 'blue', 'black', 'white', 'purple'),
    'name_02' => array('red', 'blue', 'white', 'green'),
    'name_03' => array('blue', 'pink', 'purple', 'blue'),
    'name_04' => array('white', 'black', 'red'),
    'name_05' => array('yellow', 'white', 'pink', 'black')
);
例如,pink搜索应该返回
array('name\u 03','name\u 05')
一个简单的
foreach()
在数组中(
就足够了

$search = 'pink';

foreach($test as $key=>$arr){
   if(in_array($search,$arr)){
     echo $key.PHP_EOL;
   }

}
输出:-

如果要将数组作为输出:-

可以在\u array()中使用


one
foreach()
非常抱歉,但对我来说,“做X的最佳方式是什么”的问题总是转化为“我已经尽了最大努力尝试自己解决这个问题;相反,我现在希望你列出所有可能的方式,并且在你做的时候你能帮我排列它们吗”——答案是,不,对不起,你首先需要自己努力。请去读一读。@CBroe我没有收到你的抱怨。为了使问题尽可能简短,我没有包括我自己的解决方案(foreach()和in_array())。=>我想可能会有一个更好/更短/更快的解决方案,使用一个我可能不知道的php函数。如果我们不知道您当前的解决方案是什么,我们怎么可能知道其他解决方案是否更快?“让问题尽可能简短”-是谁或是什么让你认为这是你的问题首先应该追求的主要目标?非常感谢much@Phantom因此,我也编辑了阵列的解决方案。看一看很高兴帮助你:):)
$test = array(
    'name_01' => array('yellow', 'red', 'blue', 'black', 'white', 'purple'),
    'name_02' => array('red', 'blue', 'white', 'green'),
    'name_03' => array('blue', 'pink', 'purple', 'blue'),
    'name_04' => array('white', 'black', 'red'),
    'name_05' => array('yellow', 'white', 'pink', 'black')
);

print_r(find_keys($test, 'pink'));

function find_keys($arr, $find){
    $keys = array();
    foreach ($arr as $key => $value) {
        if (!in_array($find, $value)) {
            continue;
        }
        $keys[] = $key;
    }

    return $keys;
}