2014-02-16 37 views
6

是否有任何PHP函数,例如in_array为mysql函数“mysql_fetch assoc”获取的关联数组?如何在关联数组中使用PHP in_array?

举例来说,如果我有一个$阵列看起来像这样:

array(0=>(array(ID=>1, name=>"Smith"), 1=>(array(ID=>2, name=>"John")) 

我可以这样做in_array(key,value,array)?

或者对于我来说,如果我要寻找的“ID值1“,in_array("ID",1,$array)

这是我的解决方案,如果你认为这是正确的方式对其进行评论:

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; 
    } 
} 
+0

我想你想的键/值对您的给定值进行比较。对? –

回答

9

试试这个..... 您可以使用此功能相关联的阵列中的任何深度。与此功能不同的是,键值不会在数组中的任何位置重复。

<?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')); 
?> 
+2

这是令人失望的,这是公认的答案。 –

+0

我不udnerstand为什么你写一个新的代码,当存在对一个内置功能。你可以在下面看到它。另一方面,'$ k'变量会浪费内存空间,因为它应该只检查值。 – PumpkinSeed

+0

@AndrewJarvis虽然已经有一段时间了,你为什么这么说呢?当我们知道密钥和数组是以这种方式设计的时候,我们使用了 –

6

你不能直接做它在嵌套的数组。您需要嵌套下来了一点,然后做它。

<?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 :

Yes found.. and the correspoding key is ID and the employee is Smith 
+0

猜猜我只需要为它编写自己的函数,那么吧? –

+0

只是做一个'foreach',你可以在里面做。 –

+0

我编辑了我的文章,查看代码并告诉我您是否认为它的正确性,或者是否有办法让它变得更好。 –

4

首先你要知道你要在in_array功能草垛要使用的关联数组的一部分。然后,您可以使用in_array而无需额外的代码。

例与价值观:

<?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>"; 
?> 

结果:

Array ([1] => apple [2] => banana [3] => lemon [4] => pear) 

banana : TRUE 

cherry : FALSE 
20

在你的情况,我不知道如果简单地使用isset()函数更有意义,即

isset($a[$key]) 
+0

。有时我们不得不搜索 – rajangupta

0

的样品,使用类别和方法:

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); 

Model in ideone