[英]PHP Array Search with key values
I have following arrays : 我有以下数组:
<?php
$a=array(
"abc"=>array("red","white","orange"),
"def"=>array("green","vilot","yellow"),
"xyz"=>array("blue","dark","pure")
);
echo array_search(array("dark"),$a);
?>
How to get output of xyz in array list. 如何在数组列表中获取xyz的输出。
array_search
returns false or the key. array_search
返回false或键。 Since you have multiple dimensions you must loop through to get the lowest level. 由于您有多个维度,因此您必须遍历以获得最低级别。
Since we are in another dimension your return will actually be 1
. 由于我们处于另一个维度,您的回报实际上将为1
。 For this reason, if array_search
succeeds we must use the key that is defined in the foreach 因此,如果array_search
成功,则必须使用foreach中定义的键
<?php
$a=array("abc"=>array("red","white","orange"),"def"=>array("green","vilot","yellow"),"xyz"=>array("blue","dark","pure"));
foreach($a as $key=>$data){
if(array_search("dark",$data)){
echo $key;
}
}
Outputs: xyz 输出:xyz
You can create one user-define function to check value 您可以创建一个用户定义函数来检查值
$a=array("abc"=>array("red","white","orange"),"def"=>array("green","vilot","yellow"),"xyz"=>array("blue","dark","pure"));
function search_data($value, $array) {
foreach ($array as $key => $val) {
if(is_array($val) && in_array($value,$val))
{
return $key;
}
}
return null;
}
echo search_data("dark",$a);
Please try this 请尝试这个
function searchMultiArray($arrayVal,$val){
foreach($arrayVal as $key => $suba){
if (in_array($val, $suba)) {
return $key;
}
}
}
$a = array("abc"=>array("red","white","orange"),"def"=>array("green","vilot","yellow"),"xyz"=>array("blue","dark","pure"));
echo $keyVal = searchMultiArray($a , "dark");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.