php 在多维数组中使用 in_array 搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8727984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
search with in_array in multidimensional array
提问by fteinz
My English issnt so good sorry for that.
我的英语不是很好很抱歉。
i have a array:
我有一个数组:
Array ( [name] => Array
(
[0] => Sorry the name is missing!
[1] => Sorry the name is to short!
)
)
Now I want to test with in_array for eg "name".
现在我想用 in_array 测试例如“名称”。
if (in_array("name", $false["name"])) {
echo "the array keys";
}
but it dsnt work. could annybody help me please? thanks a lot.
但它不工作。annybody可以帮帮我吗?多谢。
回答by stefandoorn
回答by Subail Sunny
in_array()
does not work with multi-dimensional arrays, so it is not possible to use in_array()
here. When you are searching "name" in in_array()
it searches in the first array and finds a key of the array named "name".
in_array()
不适用于多维数组,因此无法在in_array()
此处使用。当您在其中搜索“name”时,in_array()
它会在第一个数组中搜索并找到名为“name”的数组的键。
Better to use array_key_existsfunction. An example is given below: (remember it is only a suggestion. code may vary)
最好使用array_key_exists函数。下面给出了一个例子:(记住这只是一个建议。代码可能会有所不同)
if(array_key_exists('name', $false['name'])) {
echo $false['name'][0]; // or [1] .. or whatever you want to echo
}
//$false['name'] array contains your searched data in different keys; 0,1,2,....
You can use foreach()
to loop the first array then search using in_array()
but that will not be a good method because it will take more time to find.
Best of luck :)
您可以使用foreach()
循环第一个数组然后使用搜索,in_array()
但这不是一个好方法,因为查找需要更多时间。
祝你好运:)
回答by norman784
Maybe you need to walk through the array first, then check it
也许你需要先遍历数组,然后检查它
function in_multiarray($str, $array)
{
$exists = false;
if (is_array($array)) {
foreach ($array as $arr):
$exists = in_multiarray($str, $arr);
endforeach;
} else {
echo $array . ' = ' . $str . "\n";
if (strpos($array, $str) !== false) $exists = true;
}
return $exists;
}
回答by Treffynnon
in_array()
looks for the exact value. So you would need to specify "Sorry the name is missing!"
instead of just "name"
.
in_array()
寻找确切的值。因此,您需要指定"Sorry the name is missing!"
而不仅仅是"name"
.
For example:
例如:
if (in_array("Sorry the name is missing!", $false["name"])) {
echo "the array keys";
}
Where:
在哪里:
$false = array( 'name' => array(
0 => 'Sorry the name is missing!',
1 => 'Sorry the name is to short!'),
);
回答by Naveed
If you are searching for array key namein your main array:
$arr = array( "name" => array(
"0" => "Sorry the name is missing!",
"1" => "Sorry the name is to short!"
));
if(array_key_exists('name', $arr)) {
print_r( $arr['name'] );
} else {
echo "array key not found";
}
It will not find nameif you will search in $arr['name']because it contains only 0and 1array keys at this level.
如果您在$arr['name'] 中搜索,它将找不到name,因为它在此级别仅包含0和1数组键。
回答by iCezz
If you are wanting to use in_array you just need to put the second parameter as the array you have defined example
如果你想使用 in_array 你只需要把第二个参数作为你定义的数组例子
if(in_array('name',$false){
//do your stuff
print_r($false['name']); //this will print the array of name inclusive of [0] and [1]
}
There are more example given in the in_array php manual. please check it out.
in_array php 手册中给出了更多示例。请检查一下。