我的数组看起来像这样
$langs = Array
(
[source] => Array
(
[0] => Array
(
[0] => Arabic
)
[1] => Array
(
[0] => Azerbaijani
)
)
[target] => Array
(
[0] => Azerbaijani
[1] => Array
(
[0] => Amharic
[1] => Burmese
)
[2] => Array
(
[0] => English
[1] => German
)
)
)
现在我想从目标键中搜索一个值。所以我的代码看起来像这样
$target = array();
array_push($target, 'English'); // want to search from the above array so I made it push to array
foreach( $langs['target'] as $langs ) {
if(in_array( $target, $langs )) {
echo 'Got the value';
}
else {
echo 'not got values';
}
}
但它不起作用。那么有人可以告诉我如何获得价值观吗?任何帮助和建议都会非常明显。感谢
答案 0 :(得分:0)
试试这个:
$got=false;
foreach( $langs['target'] as $lan=> $langs) {
foreach( $langs as $lang=> $lan){
if($target[0]==$lan ) {
$got=true;
break;
}else{
continue;
}
}
}
if($got){
echo "got it";
}else{
echo "not found";
}
注意: in_array不适用于多维数组。
答案 1 :(得分:0)
你必须递归地执行此操作,遍历每个数组,因为in_array不会递归搜索。
试试这个:
$target = array(
'Azerbaijani',
array('Amharic','Burmese'),
array('English','German')
);
function search_recursive($find, $where) {
foreach ($where as $element) {
if (is_array($element)) {
if (search_recursive($find, $element))
return true;
}
elseif ($find == $element) {
return true;
}
}
return false;
}
if (search_recursive('English', $target)) echo "FOUND";
else echo "Not Found";
尚未经过全面测试,因此请确保您正确测试。