2011-12-22 48 views
0

所以我有這個數組:PHP比較兩個數組,但他們(或ALT方法)周圍的foreach

Array 
(
    [0] => Array 
     (
      [Page] => Array 
       (
        [id] => 2 
       ) 

      [PageRevision] => Array 
       (
        [PageId] => 1 
        [PageRevisionId] => 3 
        [goldMaster] => 0 
       ) 

      [PageLanguage] => Array 
       (
        [name] => Contact Page 
       ) 

      [PageSetting] => Array 
       (
        [url] => contact2 
       ) 

     ) 

    [1] => Array 
     (
      [Page] => Array 
       (
        [id] => 2 
       ) 

      [PageRevision] => Array 
       (
        [PageId] => 1 
        [PageRevisionId] => 2 
        [goldMaster] => 1 
       ) 

      [PageLanguage] => Array 
       (
        [name] => Contact Page 2 
       ) 

      [PageSetting] => Array 
       (
        [url] => contact 
       ) 

     ) 

) 

什麼,我需要做的是確定,從給出的兩個數組,如果他們中的一個有比另一個更高的PageRevisionId,如果它的goldMaster設置爲0.但是我正在努力尋找任何方法來做到這一點。

回答

1
if ($array[0]['PageRevision']['PageRevisionId'] > $array[1]['PageRevision']['PageRevisionId'] 
&& $array[0]['PageRevision']['goldMaster'] == 0) { 
    // your code 
} 
+0

但我不知道他們會在什麼樣的順序,所以$數組2可能有goldMaster設置爲0,我不知道,但謝謝! – benhowdle89 2011-12-22 17:18:25

1

您可以在您使用此代碼與無限數量的元素的主陣列:

$max_pageRevisionId = 0; 
$max_goldMaster = 0; 

foreach($myarray as $key => $value) { 
    if($value['PageRevision']['PageRevisionId'] > $max_pageRevisionId) { 
     $max_pageRevisionId = $value['PageRevision']['PageRevisionId']; 
     $max_goldMaster = $value['PageRevision']['goldMaster']; 
    } 
} 

if($max_goldMaster > 0) { 
    // Do something 
} else { 
    // Do something else 
} 
0
$elem = ($array [0]['PageRevision']['PageRevisionId'] > $array [1]['PageRevision']['PageRevisionId'])? 
1: 
0; 

var_dump ($array [0]['PageRevision']['goldMaster '] == 0)