[英]Searching a value inside a multidimensional array in php
I'm currently using this: 我目前正在使用此:
foreach($hash_list as $key => $val){
if(in_array($search_this,$hash_list[$key])){
echo 'Found value in key '.$key;
break;
}
}
To find $search_this
in this: 要在其中找到
$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");
Is there a better way to find the key for this situation? 有没有更好的方法来找到这种情况的关键?
$hash_list
has about 500 arrays with 5 elements each inside. $hash_list
包含约500个数组,每个数组中包含5个元素。
Deadpool: @Sergey No php built-in function that I'm missing? Deadpool:@Sergey我不缺少php内置函数吗?
I dont think so. 我不这么认为。 But I think it's may be little faster, but I'm not sure
但我认为速度可能会快一点,但我不确定
$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;
}
}
You can make use of list()
too ;) 您也可以使用
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";
}
}
OUTPUT : 输出:
67s6q found in c
You can use user build functions like this 您可以使用这样的用户构建功能
function isValueExist($hash_list,$needle){
foreach($hash_list as $val){
if(in_array($needle,array_values($val))) return 1;
}
}
Usage :- 用法:-
if (isValueExist($hash_list,"d2l9f")){
//DO you things here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.