2013-05-18 25 views
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()搞砸了一點,但沒有任何結果。有任何想法嗎?

+0

您可以用'in_array();' –

+0

我試過in_array(),我想最好的解決方案,佔所有我得到的答案是foreach循環.. – Mustapha

+0

結帳我的答案。我使用了'foreach()'和'in_array()' –

回答

3

在這種情況下,PHP非常笨拙。全能最好的解決方案是一個簡單的foreach

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

if ($found) .... 

您可以在許多性感的方式寫這篇文章,但他們都將需要額外的內存分配和/或會慢一些;如果你不是很有經驗,他們中的很多人也會更難以理解。

+0

@PeeHaa埽不,他沒有,代碼是絕對正確的? – bwoebi

+0

只有當你知道最後一個鍵的名字是什麼時,這纔會起作用。如果該陣列有1000個頂級鍵,並且您想查看其中是否包含「橙色」的任何位置,該怎麼辦?如果名字是'橘子',顏色是'橙色',這段代碼會錯過它。該陣列是結構化的。 $ fruits [0] ['name'] ==蘋果。如果你沒有經過[0,1,2 ...等],它將無法工作,因爲第一個foreach循環僅在第一個鍵上用一個定義鍵迭代第一個鍵。 –

+0

@Unipartisandev:我不知道你在說什麼。這個問題可以通過名稱來尋找成果*。我們不關心任何其他密鑰。 – Jon

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

?> 

Codepad Demo>>

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

+0

你能解釋一下你的代碼嗎? – Fabio

+0

我測試過它,它工作。它比我的答案代碼短,並且它不同於其他答案在多個密鑰級別上迭代。 –

+0

@Fabio:更新瞭解釋並解釋。如果你仍然有疑問,我很樂意幫助 –

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")); 
+0

對'array_column' +1,雖然有一個巨大的警告:你最好確定它不是一個大陣列,因爲你將支付帶血的單線加可讀性便利性。 ;-) – Jon

+0

@Jon對於小數組,array_column大約慢了3倍,但對於大數組只有2次(我只是對它進行基準測試......) – bwoebi

+0

@Jon'php bench_array_column_in_array.php => array_column:243 secs foreach :122 secs'與300萬個條目陣列和循環100次(和內存使用量是最大的雙倍大小的陣列) – bwoebi

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

?> 
+0

如果您搜索橙色,但蘋果有橙色,該怎麼辦? –

+0

它返回'true'。主要的問題是在數組中找到任何_apple_不能找到名稱爲_apple_的元素。 – furas

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循環內搜索通過擴展鍵。

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

這很好,但如果我想搜索一種顏色呢?你將不得不編輯該功能。將它設置爲:function arraySearch($ value,$ array,$ type ='name')... then if($ val [$ type] === $ value。 –

+0

不,你可以在函數arraySearch中傳遞另一個參數($ value,$ array,$ type)並用if($ val [$ type] === $ value)檢查條件 –

+0

這正是我所說的,除非我有$ type的默認值,而不是必要性。 –