在php中搜索多维数组中的值

时间:2013-12-13 05:53:43

标签: php arrays multidimensional-array

我目前正在使用它:

foreach($hash_list as $key => $val){
    if(in_array($search_this,$hash_list[$key])){
        echo 'Found value in key '.$key;
        break;
    }
}

在此处找到$search_this

$hash_list = array();
$hash_list["a"] = array("dfv8p","hi8o7","d2l9f","qhx13","c7duz");
$hash_list["b"] = array("pdsyt","jjivh","nj12b","19tm2","ltsqp");
$hash_list["c"] = array("67s6q","tlwu7","c9p77","7airj","j7tej");

有没有更好的方法来找到这种情况的关键? $hash_list有大约500个数组,每个数组中包含5个元素。

3 个答案:

答案 0 :(得分:0)

Deadpool:@Sergey没有我缺少的php内置函数?

我不这么认为。但我认为它可能会快一点,但我不确定

$hash_list = array();
$hash_list["a"] = "dfv8p, hi8o7, d2l9f, qhx13, c7duz";
$hash_list["b"] = "pdsyt, jjivh, nj12b, 19tm2, ltsqp";
$hash_list["c"] = "67s6q, tlwu7, c9p77, 7airj, j7tej";

foreach($hash_list as $key => $val){
    if(strpos($hash_list[$key], $search_this) !== false) {
        echo 'Found value in key '.$key;
        break;
    }
}

答案 1 :(得分:0)

您也可以使用list();)

<?php
$hash_list = array();
$hash_list["a"] = array("dfv8p","hi8o7","d2l9f","qhx13","c7duz");
$hash_list["b"] = array("pdsyt","jjivh","nj12b","19tm2","ltsqp");
$hash_list["c"] = array("67s6q","tlwu7","c9p77","7airj","j7tej");

$searchParam = "67s6q";
while(list($a,$b) = each($hash_list))
{
if(in_array($searchParam,$b))
{
echo "$searchParam found in $a\n";
}
}

输出:

67s6q found in c

答案 2 :(得分:0)

您可以使用此类用户构建功能

function isValueExist($hash_list,$needle){
 foreach($hash_list as $val){
  if(in_array($needle,array_values($val))) return 1;
 }
}

用法: -

if (isValueExist($hash_list,"d2l9f")){
    //DO you things here
  }