繁体   English   中英

比较两个多维数组

[英]compare two multidimensional 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;

所以我想检测“用户”的东西是否丢失,然后将丢失的东西插入到第二个数组中

下面的示例将检查$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>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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