如何将 PHP in_array 与关联数组一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21809116/
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
How to use PHP in_array with associative array?
提问by Jacob Cohen
Is there any php function such as in_array for associative arrays you get by the mysql function "mysql_fetch assoc" ?
是否有任何 php 函数,例如 in_array 用于您通过 mysql 函数“mysql_fetch assoc”获得的关联数组?
For example, if I have an $array that looks like this:
例如,如果我有一个看起来像这样的 $array:
array(0=>(array(ID=>1, name=>"Smith"), 1=>(array(ID=>2, name=>"John"))
Can I do something like in_array(key,value,array)?
我可以做类似的事情吗 in_array(key,value,array)?
Or in my case, if I am looking for the ID value of "1", in_array("ID",1,$array).
或者就我而言,如果我正在寻找“1”的 ID 值,in_array("ID",1,$array).
This is my solution, comment on it if you think it's the right way:
这是我的解决方案,如果您认为这是正确的方法,请对其发表评论:
function in_assoc_array($key,$value,$array)
{
if (empty($array))
return false;
else
{
foreach($array as $a)
{
if ($a[$key] == $value)
return true;
}
return false;
}
}
回答by Phil LaNasa
In your case, I wonder if simply using isset() makes more sense, i.e.
在你的情况下,我想知道简单地使用 isset() 是否更有意义,即
isset($a[$key])
回答by Phil LaNasa
Try this..... You can use this function for any depth of the associated array. Just contraint to this function is that the key value would not be repeat any where in array.
试试这个..... 您可以将此函数用于关联数组的任何深度。对该函数的限制是键值不会在数组中的任何位置重复。
<?php
function is_in_array($array, $key, $key_value){
$within_array = 'no';
foreach( $array as $k=>$v ){
if( is_array($v) ){
$within_array = is_in_array($v, $key, $key_value);
if( $within_array == 'yes' ){
break;
}
} else {
if( $v == $key_value && $k == $key ){
$within_array = 'yes';
break;
}
}
}
return $within_array;
}
$test = array(
0=> array('ID'=>1, 'name'=>"Smith"),
1=> array('ID'=>2, 'name'=>"John")
);
print_r(is_in_array($test, 'name', 'Smith'));
?>
回答by Shankar Damodaran
You can't do it directly on nested arrays.. You need to nest it down a bit and then do it.
你不能直接在嵌套数组上做。你需要把它嵌套一下,然后再做。
<?php
$arr=array(0=>array('ID'=>1, 'name'=>"Smith"), 1=>array('ID'=>2, 'name'=>"John"));
foreach($arr as $arr1)
{
if(in_array(1,$arr1))
{
echo "Yes found.. and the correspoding key is ".key($arr1)." and the employee is ".$arr1['name'];
}
}
OUTPUT :
OUTPUT :
Yes found.. and the correspoding key is ID and the employee is Smith
回答by David Blanchard
First you must know which part of the associative array you're going to use as haystack in in_arrayfunction. Then you can use in_arraywithout additional code.
首先,您必须知道将在in_array函数中用作 haystack 的关联数组的哪一部分。然后您in_array无需额外代码即可使用。
Example with values :
带有值的示例:
<?php
$assoc = array(1 => "apple", 2 => "banana", 3 => "lemon", 4 => "pear");
$haystack = array_values($assoc);
echo "<p>" . print_r($assoc, true) . "</p>";
$needle = 'banana';
$find = (in_array($needle, $haystack)) ? 'TRUE' : 'FALSE';
echo "<p>$needle : $find</p>";
$needle = 'cherry';
$find = (in_array($needle, $haystack)) ? 'TRUE' : 'FALSE';
echo "<p>$needle : $find</p>";
?>
Results in :
结果是 :
Array ( [1] => apple [2] => banana [3] => lemon [4] => pear )
banana : TRUE
cherry : FALSE
回答by Ivan Ferrer
The sample formule, using class and methods:
示例公式,使用类和方法:
class VerifyInArray
{
public function getMyCollection($field, $collection)
{
$list = array();
if (count($collection)) {
foreach ($collection as $k => $val) {
$list[] = $val[$field];
}
}
return $list;
}
public function inMyArray($collection, $field, $findValue)
{
if (isset($collection[0])) {
if (array_key_exists($field, $collection[0]) == false) {
return 'no';
}
}
if (in_array($findValue, $this->getMyCollection($field, $collection))) {
return 'ok';
}
return 'no';
}
public function displayInArray($collection, $attr, $value)
{
return 'search result: '. $this->inMyArray($collection, $attr, $value);
}
}
$src = new VerifyInArray();
$collection = array(
array(
'ID' => 1,
'name' => 'Smith'
),
array(
'ID' => 2,
'name' => 'John'
)
);
echo $src->displayInArray($collection, 'ID', 2). "\n<br>" .
$src->displayInArray($collection, 'ID', 0);
回答by Clinton Dobbs
Here is a one liner you can use.
这是您可以使用的单衬。
$isInArray = in_array(1, array_column($names, 'ID'));

