2015-12-03 68 views
0

這是我的示例數組。我得到了另外一個像這樣的EXACT相同數量的元素(案例中有25個數組元素)。 該應用程序爲我提供了這些數組的2個ID,例如:陣列#1中的355和陣列#2中的888,我必須比較相應的父數組ID是否相等。比較兩個數組,基於PHP中的子數組元素值

如果我的數組的ID:355是來自父數組的第0個元素,就像數組#2中的888一樣。因此,要確保,如果0 = 0

陣列#1

Array 
(
[0] => Array 
    (
     [id] => 355 
     [name] => 1 
     [desc] => 1 
     [price] => 0 
    ) 

[1] => Array 
    (
     [id] => 356 
     [name] => 1 
     [desc] => 2 
     [price] => 0 
    ) 

[2] => Array 
    (
     [id] => 357 
     [name] => 2 
     [desc] => 3D 
     [price] => 0 
    ) 
... 

這裏是陣列#2

Array 
(
[0] => Array 
    (
     [id] => 888 
     [name] => 15 
     [desc] => 1D 
     [price] => 0 
    ) 

[1] => Array 
    (
     [id] => 889 
     [name] => 16 
     [desc] => 2D 
     [price] => 0 
    ) 

[2] => Array 
    (
     [id] => 890 
     [name] => 17 
     [desc] => 3D 
     [price] => 0 
    ) 
... 

我真的無法弄清楚如何解釋更好。

歡迎任何想法。

回答

2
$match = true; 
foreach ($array1 as $key => $value) { 
    foreach ($value as $subkey => $subvalue) { 
     if ($array1[$key][$subkey] != $array2[$key][$subkey]) { 
      $match = false; 
     } 
    } 
} 

if (false === $match) { 
    // The arrays are not the same 
}