2012-12-09 33 views
2

試圖匹配兩個數組中的值,如果匹配值存在,那麼輸出true,可能是array_intersect函數?非常不確定!任何幫助深表感謝!試圖匹配兩個數組中的值,如果匹配值存在,那麼輸出true

基本上有兩個SQL查詢,我不能很適合這個盒子!但他們每個返回一個數組$ staffExpertise和$ moduleExpertise,但即時通訊非常新的PHP,甚至更不熟悉數組相交功能,所以不太確定從這裏做什麼!

foreach ($results as $row) { 
     $staffExpertise = $row['expertise']; 
    } 

    foreach ($results as $row) { 
     $moduleExpertise = $row['expertise']; 
    } 

    $arrayIntersection = array_intersect($moduleExpertise, $staffExpertise); 

    if($arrayIntersection =){ 

    } 
+0

基本上有兩個sql查詢,我不能很適合這個盒子!但他們每個返回一個數組$ staffExpertise和$ moduleExpertise,但即時通訊非常新的PHP,甚至更不熟悉數組相交功能,所以不太確定從這裏做什麼! foreach($ results as $ row){ $ staffExpertise = $ row ['expertise']; } foreach($ results as $ row){ $ moduleExpertise = $ row ['expertise']; } $ arrayIntersection = array_intersect($ moduleExpertise,$ staffExpertise); if($ arrayIntersection =){ } –

+1

@FindlayMack使用正確的格式更新您的問題。 –

+0

向我們展示您的一些嘗試代碼,或者我們無法提供幫助。 – DrinkJavaCodeJava

回答

0

嘗試:

<?php 
    if (in_array($Array1, $VarToCheck)) { 
     $return = true; 
    }elseif (in_array($Array2, $VarToCheck)){ 
    $return = true; 
    } 
?> 
+0

感謝您的迴應,但問題是,因爲$ vartocheck是從兩個單獨的表中採取的,所以我不能像這樣輸入它們。例如,檢查講師是否具有正確的模塊專業知識來教授模塊;我檢查的方式是查看moduleexpertise表和staffExpertise是否具有匹配的值。如果那有意義的話? –

+0

這樣做。檢查AaronSantos的迴應,我相信這可能會訣竅! –

+0

是的,我認爲它!歡呼所有的幫助! –

4

是陣列相交做的。嘗試這樣的:

$names_1 = array("Alice", "Bob", "Charlie", "David"); 
$names_2 = array("Alice", "Bruno"); 

$intersection = array_intersect($names_1, $names_2); 

if(!empty($intersection)){ 
    echo "The following item(s) exist in both arrays:"."<br>"; 
    foreach($intersection as $row){ 
     echo $row."<br>"; 
    } 

}else{ 
echo "The arrays do not intersect"; 
} 
+0

非常感謝你,這是我後來的代碼的滑線! –

相關問題