2014-04-24 178 views
-1

嗨,我有一個這樣的數組...如何在二維數組中的元素比較PHP

array(
    array(
     1, 3 
    ), 
    array(
     4, 6, 8 
    ), 
    array(
     2, 3, 5, 1 
    ) 
) 

現在我想第一個元素的所有元素在第二row.means我想比較比較1與4,6和8.然後與第3行元素如1與2,3,5和1.同樣我想比較

1在給定數組中總共存在兩次....所以變量count1 = 2 ....同樣地3存在2次因此count2 = 2 ... 8只存在一次因此count8 = 1 ....像這樣...

Kind ly幫我解決這個問題。提前感謝。

+0

是這些不同的陣列或只是一個陣列? – Guns

+0

One array ...... – SSS

+0

所有這些比較後會發生什麼? –

回答

2

如果你只是一個頻率表後,你可以用array_merge()壓平,並使用array_count_values()得到了提示:

print_r(array_count_values(call_user_func_array('array_merge', $array))); 

輸出:

Array 
(
    [1] => 2 
    [3] => 2 
    [4] => 1 
    [6] => 1 
    [8] => 1 
    [2] => 1 
    [5] => 1 
) 
+0

非常感謝你.... – SSS

0

檢查This answer

或示例

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("Mac", "NT"), array("Irix", "Linux")); 
echo in_array_r("Irix", $b) ? 'found' : 'not found'; 
+0

如何在二維數組中實現這個? – SSS

+0

檢查更新回答 – SagarPPanchal