2010-10-13 66 views
2

我有兩個多維數組。與相同的數據......發生什麼事是。在一個數組值可以在內部一個多維數組來改變..這是我的陣列...PHP:如何查找二維數組的差異?

$previousA = array(); 
$previousA["t"] = array("twitter","picasa"); 
$previousA["d"] = array("youtube","gmail"); 


$freshA = array(); 
$freshA["t"] = array("twitter","picasa","gmail"); 
$freshA["d"] = array("youtube"); 

在任何時間總價值將會是四個「twitter」,「picasa」,「youtube」,「gmail」,它們可以從$ previousA["t"] < =>$previousA["d"] =>$previousA["d"]現在我想知道哪些值從$previousA["t"]變爲$ previousA["d"]與$ freshA

+0

您希望在您的示例中輸出什麼輸出? – codaddict 2010-10-13 05:38:43

+0

我想知道gmail從[「d」] => [「t」]從previousA – Rajesh 2010-10-13 05:40:31

回答

1

您可以利用陣列差異函數array_diff作爲:

$from_d_to_t = array_diff($freshA["t"],$previousA["t"]); 
$from_t_to_d = array_diff($freshA["d"],$previousA["d"]); 

if($from_d_to_t) { 
     echo "Elements moved from d to t are ".implode(',',$from_d_to_t)."\n"; 
} 
if($from_t_to_d) { 
     echo "Elements moved from t to d are ".implode(',',$from_t_to_d)."\n"; 
} 

Working code

0

這就是簡單,所有你需要做的是環通的多維度陣列(我假設是2階),並使用功能array_diff比較子陣列。此功能給出了兩個陣列即額外的記錄之間的差異在陣列

0

如果你的元素必須是無論是在td那麼你真的只需要跟蹤的td之一。

ex) 
$previousT = array('twitter', 'picasa'); 
$currentT = array('picasa', 'gmail'); 

then 

$d_to_t = array_diff($currentT, $previousT); // 'gmail' 
$t_to_d = array_diff($previousT, $currentT); // 'twitter'