PHP比较两个数组但是围绕它们(或alt方法)

时间:2011-12-22 17:06:59

标签: php arrays

所以我有这个数组:

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.但是我很难找到任何方法来执行此操作

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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
}

答案 2 :(得分:0)

$elem = ($array [0]['PageRevision']['PageRevisionId'] > $array [1]['PageRevision']['PageRevisionId'])?
 1:
 0;

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