2014-07-21 62 views
2

我試圖採取號碼兩個列表(陣列),並使用在FOREACH PHP環路來創建包含所有常見的號碼的第三陣列。感覺就我理解邏輯而言,我碰到了一個障礙,那麼任何人都可以理解我所得到的以及出了什麼問題?使用嵌入式foreach循環至交叉參考陣列在PHP

<?php 
$cardOutcome = array(); 

$explodeCalled = explode(",", $card_detail['calledNumbers']); 
$explodeCard = explode(",", $card_detail['cardNumbers']); 

foreach($explodeCard as $card) 
{ 
    foreach($explodeCalled as $called) 
    { 
     if($called == $card) 
     { 
      $cardOutcome[] = $called; 
     } 
    } 
} 

print_r($cardOutcome); 
?> 

數組值和print_r的成果低於

$explodeCard = 2, 5, 1, 11, 4, 25, 19, 28, 21, 18, 32, 37, 38, 41, 40, 50, 55, 49, 56, 57, 75, 73, 61, 72, 74 

$explodeCalled = 3, 70, 39, 6, 45, 43, 9, 48, 54, 51, 49, 33, 21, 1, 65, 71, 75, 15, 50, 36, 55, 14, 13, 7, 4, 67, 74, 26, 8, 32, 22, 28, 62, 57, 56, 38, 12, 2, 59, 40, 27, 69, 18, 20, 42, 44, 37, 46, 72, 60, 11, 58, 66, 29, 30, 35, 52, 25, 47, 10, 61, 5, 23, 73, 53, 34, 41, 

$cardOutcome = Array ([0] => 2 [1] => 5 [2] => 1 [3] => 11 [4] => 4 [5] => 25 [6] => 28 [7] => 21 [8] => 18 [9] => 32 [10] => 37 [11] => 38 [12] => 41 [13] => 40 [14] => 50 [15] => 55 [16] => 49 [17] => 56 [18] => 57 [19] => 75 [20] => 73 [21] => 61 [22] => 72 [23] => 74) 

的$結果陣列似乎是簡單打印「卡」的值。我覺得我已經做出了明顯的邏輯失誤。和想法?

回答

3

嘗試array_intersect(http://php.net/manual/en/function.array-intersect.php

$explodeCalled = explode(",", $card_detail['calledNumbers']); 
$explodeCard = explode(",", $card_detail['cardNumbers']); 
$cardOutcome = array_intersect($explodeCalled,$explodeCard); 
+0

優秀的,正是我需要的,謝謝! – user3520983