简体   繁体   中英

compare two multidimensional arrays

i have this array:

        $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;

and now i want to compare this with another array to check, if there is something missing in the 2nd array.

        $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;

so i want to detect that the 'user' stuff is missing and then insert the missing stuff into the 2nd array

The example below will check that $userArray contains ALL the fields at the top level of $default . If something is missing, it will add it. By doing it this way, you will not be overwriting any fields (at the top level) that the user has specified.

<?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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM