比较两个多维数组

时间:2018-11-07 10:16:10

标签: php arrays

我有这个数组:

        $default['products']['categories']['list'] = 0;
    $default['products']['categories']['search'] = 0;
    $default['products']['categories']['delete'] = 0;
    $default['products']['categories']['create'] = 0;
    $default['products']['categories']['edit'] = 0;

    $default['products']['product']['list'] = 0;
    $default['products']['product']['search'] = 0;
    $default['products']['product']['delete'] = 0;
    $default['products']['product']['create'] = 0;
    $default['products']['product']['edit'] = 0;
    $default['products']['product']['info'] = 0;
    $default['products']['product']['get_barcode_img'] = 0;
    $default['products']['product']['update_amount'] = 0;

    $default['api']['version'] = 0;

    $default['user']['create'] = 0;
    $default['user']['delete'] = 0;
    $default['user']['resetpassword'] = 0;

现在我想将它与另一个数组进行比较,以检查第二个数组中是否缺少某些内容。

        $2nd['products']['categories']['list'] = 0;
    $2nd['products']['categories']['search'] = 0;
    $2nd['products']['categories']['delete'] = 0;
    $2nd['products']['categories']['create'] = 0;
    $2nd['products']['categories']['edit'] = 0;

    $2nd['products']['product']['list'] = 0;
    $2nd['products']['product']['search'] = 0;
    $2nd['products']['product']['delete'] = 0;
    $2nd['products']['product']['create'] = 0;
    $2nd['products']['product']['edit'] = 0;
    $2nd['products']['product']['info'] = 0;
    $2nd['products']['product']['get_barcode_img'] = 0;
    $2nd['products']['product']['update_amount'] = 0;

    $2nd['api']['version'] = 0;

所以我想检测到'user'东西丢失了,然后将缺失的东西插入第二个数组中

1 个答案:

答案 0 :(得分:0)

下面的示例将检查$userArray是否包含$default顶层的所有字段。如果缺少某些内容,它将添加它。通过这种方式,您将不会覆盖用户指定的任何字段(在顶层)。

<?php

//Your array of default permissions
$default = array(
        "products" => array(
                "categories" => array(
                        "list" => 0,
                        "search" => 0,
                        "delete" => 0,
                        "create" => 0,
                        "edit" => 0,
                    ),
                "product" => array(
                        "list" => 0,
                        "search" => 0,
                        "delete" => 0,
                        "create" => 0,
                        "edit" => 0,
                        "info" => 0,
                        "get_barcode_img" => 0,
                        "update_amount" => 0,
                    )
            ),
        "api" => array(
                "version" => 0,
            ),
        "user" => array(
                "create" => 0,
                "delete" => 0,
                "resetpassword" => 0,
            ),          
    );


//The user specified array of permissions, missing everything at "user" section
$userArray = array(
        "products" => array(
                "categories" => array(
                        "list" => 0,
                        "search" => 0,
                        "delete" => 0,
                        "create" => 0,
                        "edit" => 0,
                    ),
                "product" => array(
                        "list" => 0,
                        "search" => 0,
                        "delete" => 0,
                        "create" => 0,
                        "edit" => 0,
                        "info" => 0,
                        "get_barcode_img" => 0,
                        "update_amount" => 0,
                    )
            ),
        "api" => array(
                "version" => 0,
            ),      
    );

foreach( $default as $key=>$group ){
    if( !isset( $userArray[$key] )) {
        $userArray[ $key ] = $group;
    }
}

echo "<pre>" . print_r( $userArray, 1 ) . "</pre>";