2011-07-08 101 views
0

我們想知道,我們如何使用數組(關鍵字)檢查數組(數據)。我們希望將結果存儲到變量中以供進一步使用。 這裏我們詳細解釋你。數組檢查複製和唯一值,並打印它們

 
$array1 = array(John Wilkins, Poul Warner, Rodger Smith, David Bentham, David Wilkins, Brian Smith, David Warner)(Data) 

$array2 = array(Wilkins, Warner, Smith, Bentham)(Keywords) 

這裏我們想檢查array1中的array2。並將結果存儲在變量中,並在稍後或結束時打印出來。

 
Checking Process will be....... 
Array2(Wilkins) checks all the items in Array1. 
Array2(Warner) checks all the items in Array1. 
Array2(Smith) checks all the items in Array1. 
and so on... 

請幫助解決這個問題..

感謝 ROD

+0

'Array2'中的'foreach'項目在'Array1'中做了'foreach'項目,所以你會有一個嵌套的'foreach'循環。可能有任何幫助? – Joost

+0

我們正在做這種類型的事情。我們沒有得到那個價值。 – PPS

回答

0

怎麼是這樣的:

$matches = array(); 

foreach ($array2 as $value2) 
    foreach ($array1 as $value1) 
    if (stripos($value1, $value2) !== FALSE) 
     $matches[] = $value1; 

// Just in case a record matched more than once. 
$matches = array_unique($matches); 
0
$array1 = array("John Wilkins", "Poul Warner", "Rodger Smith", "David Bentham", "David Wilkins", "Brian Smith", "David Warner"); 
$array2 = array("Wilkins", "Warner", "Smith", "Bentham"); 
$result = array(); 

foreach ($array2 as $value) { 
    $result = array_merge(preg_grep("/$value/", $array1), $result); 
} 

$result = array_unique($result); 

print_r($result); 

輸出:

Array 
(
    [0] => David Bentham 
    [1] => Rodger Smith 
    [2] => Brian Smith 
    [3] => Poul Warner 
    [4] => David Warner 
    [5] => John Wilkins 
    [6] => David Wilkins 
) 
相關問題