zoukankan      html  css  js  c++  java
  • array_diff函数的注意事项

      array_diff — 计算数组的差集

      说明:

      array array_diff ( array $array1 , array $array2 [, array $... ] )  对比返回在 array1 中但是不在 array2 及任何其它参数数组中的值。注意键名保留不变。

      注意:本函数只检查了多维数组中的一维。如果想比较更深的维度需要另写一个函数,今天的工作就遇到了这样的需求,所以写了一个函数来比较更深的维度。

      

    <?php
    header("Content-type:text/html;charset=utf-8");
    $json1='{ "filedir":"default", "pages" : [ { "name" : "首页", "blocks":[ { "name":"头部标题栏", "blocktype":"title_bar", "settings":{ "is_show":true, "bg_color":"#1eb7a4", "content_switch":true, "content":"", "bg_url":"", "color":"#fff", "border_bottom_color":"", "border_bottom_width":"0" } }, { "name":"头部广告图", "blocktype":"ad_picture", "settings":{ "is_show":true, "bg_url":"" } }, { "name":"广告", "blocktype":"ad", "settings":{ "is_show":true, "number":5, "show_type":"scroll" } }, { "name":"菜单", "blocktype":"menu", "settings":{ "is_show":true, "bg_color":"#fff", "color":"#1eb7a4" } }, { "name":"个人中心", "blocktype":"personal_center", "settings":{ "is_show":true, "bg_color":"#fff", "color":"#1eb7a4" } }, { "name":"上网按钮", "blocktype":"online_button", "settings":{ "is_show":true, "offline_bg_url":"", "online_bg_url":"" } } ] }, { "name" : "登录页", "blocks":[ { "name":"页面背景", "blocktype":"page_bg", "settings":{ "is_show":true, "bg_url":"", "bg_color":"" } }, { "name":"logo图", "blocktype":"logo", "settings":{ "is_show":true, "bg_url":"" } }, { "name":"登录模块", "blocktype":"login", "settings":{ "is_show":true, "success_url":"" } } ] }, { "name" : "认证过程页", "duration":"5", "blocks":[ { "name":"页面背景", "blocktype":"page_bg", "settings":{ "is_show":false, "bg_url":"" } }, { "name":"登录动画", "blocktype":"login_animate", "settings":{ "is_show":true, "bg_url":"" } } ] }, { "name" : "登录成功页", "blocks":[ { "name":"头部广告图", "blocktype":"ad_picture", "settings":{ "is_show":true, "bg_url":"" } }, { "name":"成功页app", "blocktype":"apps", "settings":{ "is_show":true } }, { "name":"成功页提示信息", "blocktype":"success_tips", "settings":{ "is_show":false, "color":"#fff", "content":"" } } ] }, { "name" : "广告细览页", "blocks":[ { "name":"头部标题栏", "blocktype":"title_bar", "settings":{ "is_show":true, "bg_color":"#1eb7a4", "content_switch":true, "content":"", "bg_url":"", "color":"#fff", "border_bottom_color":"", "border_bottom_width":"0" } } ] } ] }';
    
    $json2='{ "filedir":"default", "pages" : [ { "name" : "首页", "blocks":[ { "name":"头部标题栏", "blocktype":"title_bar", "settings":{ "is_show":true, "bg_color":"#1eb7a4", "content_switch":true, "content":"", "bg_url":"", "color":"#fff", "border_bottom_color":"", "border_bottom_width":"0" } }, { "name":"头部广告图", "blocktype":"ad_picture", "settings":{ "is_show":true, "bg_url":"" } }, { "name":"广告", "blocktype":"ad", "settings":{ "is_show":true, "number":5, "show_type":"scroll" } }, { "name":"菜单", "blocktype":"menu", "settings":{ "is_show":true, "bg_color":"#fff", "color":"#1eb7a4" } }, { "name":"个人中心", "blocktype":"personal_center", "settings":{ "is_show":true, "bg_color":"#fff", "color":"#1eb7a4" } }, { "name":"上网按钮", "blocktype":"online_button", "settings":{ "is_show":true, "offline_bg_url":"", "online_bg_url":"" } } ] }, { "name" : "登录页", "blocks":[ { "name":"页面背景", "blocktype":"page_bg", "settings":{ "is_show":true, "bg_url":"", "bg_color":"" } }, { "name":"logo图", "blocktype":"logo", "settings":{ "is_show":true, "bg_url":"" } }, { "name":"登录模块", "blocktype":"login", "settings":{ "is_show":true, "success_url":"" } } ] }, { "name" : "认证过程页", "duration":"5", "blocks":[ { "name":"页面背景", "blocktype":"page_bg", "settings":{ "is_show":false, "bg_url":"" } }, { "name":"登录动画", "blocktype":"login_animate", "settings":{ "is_show":true, "bg_url":"" } } ] }, { "name" : "登录成功页", "blocks":[ { "name":"头部广告图", "blocktype":"ad_picture", "settings":{ "is_show":true, "bg_url":"" } }, { "name":"成功页app", "blocktype":"apps", "settings":{ "is_show":true } }, { "name":"成功页提示信息", "blocktype":"success_tips", "settings":{ "is_show":false, "color":"#fff", "content":"" } } ] }, { "name" : "广告细览页", "blocks":[ { "name":"头部标题栏", "blocktype":"title_bar", "settings":{ "is_show":true, "bg_color":"#1eb7a4", "content_switch":true, "content":"", "bg_url":"", "color":"#fff", "border_bottom_color":"", "border_bottom_width":"0" } } ] } ] }';
    
    $array1=json_decode($json1,true);
    $array2=json_decode($json2,true);
    
    
    function array_recursive_diff($array1, $array2) {
        $result = array();
        foreach ($array1 as $key1 => $value1) {
            if (array_key_exists($key1, $array2)) {
                if (is_array($value1)) {
                    $diff = array_recursive_diff($value1, $array2[$key1]);
                    if (count($diff)) { //这个位置进行优化:判断!empty($diff)
                        $result[$key1] = $diff;
                    }
                } else {
                    if ($value1 != $array2[$key1]) {
                        $result[$key1] = $value1;
                    }
                }
            } else {
                $result[$key1] = $value1;
            }
        }
    
        return $result;
    }
    
    $result=array_recursive_diff($array1, $array2);
    echo '<pre>';
    var_dump($result);
    
    if(empty($result)){
        echo '完全相同';
    }else{
        echo '完全不相同';
    }

      

      

      

    如果您阅读过此文章有所收获,请为我顶一个,如果文章中有错误的地方,欢迎指出。

    相互学习,共同进步!

     

  • 相关阅读:
    asp.net string有多行文字
    asp.net设置gridview页码显示遇到的问题
    asp.net button浏览器端事件和服务器端事件
    GridView 控制默认分页页码间距 及字体大小
    复合主键与联合主键(转载)
    vsCode 列选择、列选中、选中列、选中多列(转载)
    可能有用的技术社区(转载)
    SQL 用于各种数据库的数据类型(转载) sqlserver 数据类型 取值范围 长度
    TypeError: value.getTime is not a function (elementUI报错转载 )
    工作1年3个月总结(201707-201810 )
  • 原文地址:https://www.cnblogs.com/zhangxiaoliu/p/5051212.html
Copyright © 2011-2022 走看看