0

我有这个数组:

$fruits = array(
    [0] => array('name'=>'banana', 'color'=>'yellow' , 'shape'=>'cylinder'),
    [1] => array('name'=>'apple', 'color'=>'red' , 'shape'=>'sphere'),
    [2] => array('name'=>'orange', 'color'=>'orange' , 'shape'=>'sphere') 
    )

如何确定数组是否$fruits已包含apple在其中?

我试过了:in_array("apple", $fruits)但没有成功。

我也尝试了各种语法并弄乱了一些array_key_exists(),但没有任何结果。有任何想法吗?

4

7 回答 7

3

在这种情况下,PHP 是出了名的笨拙。最好的全方位解决方案很简单foreach

$found = false;
foreach ($fruits as $fruit) {
    if ($fruit['name'] == 'apple') {
        $found = true;
        break;
    }
}

if ($found) ....

你可以用许多更性感的方式来编写它,但它们都需要额外的内存分配和/或会更慢;如果您不是很有经验,其中很多也将更难以理解。

于 2013-05-18T19:05:08.683 回答
1
foreach($fruits as $fruit)
{ 
    /*In first loop $fruit = array('name'=>'banana', 'color'=>'yellow' ,
      'shape'=>'cylinder')
    */
    if(in_array("apple",$fruit))
    {
       /*Using in_array() function we don't have to worry about array keys. 
         The function checks whether the value exists in given array.
         Returns TRUE if value is found in the array, FALSE otherwise.
         if(in_array("apple",$fruit)) checks for TRUE
       */

       //Do something
    }
}

in_array(): http://php.net/manual/en/function.in-array.php
foreach(): http://php.net/manual/en/control-structures.foreach.php

于 2013-05-18T19:06:02.110 回答
1
$hasApple = false;
foreach ($fruits as $subArray) {
    if ($subArray['name'] == "apple")
        $hasApple = true;
}

这是正常的解决方案,很简单。您也可以尝试使用array_map($fruits, function () use (&$hasApple) { /* ... */ }). (但这可能会更慢......)

从 PHP 5.5 开始,有一种可能的方式:

$hasApple = in_array("apple", array_column($fruits, "name"));
于 2013-05-18T19:06:07.780 回答
1
<?php

$fruits = array(
    array('name'=>'banana', 'color'=>'yellow' , 'shape'=>'cylinder'),
    array('name'=>'apple', 'color'=>'red' , 'shape'=>'sphere'),
    array('name'=>'orange', 'color'=>'orange' , 'shape'=>'sphere') 
);

$result = false;

foreach($fruits as $subarray) {
    if( in_array('apple', $subarray) ) {
        $result = true;
        break;
    }
}

echo "result is ", ($result ? "TRUE" : "FALSE");

?>
于 2013-05-18T19:08:24.360 回答
0

尝试这个:

 <?PHP


function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

$b = array(array('name'=>'banana', 'color'=>'yellow' , 'shape'=>'cylinder'), array('name'=>'apple', 'color'=>'red' , 'shape'=>'sphere'),array('name'=>'orange', 'color'=>'orange' , 'shape'=>'sphere'));
echo in_array_r("apple", $b) ? 'found' : 'not found';

?>

键盘演示>>

于 2013-05-18T19:05:50.350 回答
0
$hasapple = 0;
foreach($fruits as $keynum)
{
    foreach($keynum as $fruitinfokey => $value)
        {
        if($value == "apple")
            {
            echo "fruits contains " . $value;
            $hasapple = 1;
            }
}
}

if($hasapple == 0)
    {
    echo "The array $fruits does not contain apple";
    }

在另一个 foreach 循环中放置一个 foreach 循环以搜索扩展键。

于 2013-05-18T19:11:08.107 回答
0
    function arraySearch($value, $array) {
       foreach ($array as $key => $val) {
           if ($val['name'] === $value) {
               return true;
           }
       }
       return null;
    }

if(arraySearch('apple',$fruits)){
   echo "yes found";
}else{
   echo "not found";
}
于 2013-05-18T19:11:16.633 回答