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 '完全不相同';
}

  

  

  

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

相互学习,共同进步!

array_diff函数的注意事项的更多相关文章

  1. Inline函数使用注意事项

    Inline函数使用注意事项 1.在一个文件中定义的inline函数不能再另一个文件中使用 2.inline函数应简洁,只有少数几个语句. 3.在inline函数中不能有循环,if,switch语句. ...

  2. php比较两个数组的差异array_diff()函数

    下面简单介绍php比较两个数组的差异array_diff()函数. 原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.php/archives/ ...

  3. php array_diff()函数 语法

    php array_diff()函数 语法 作用:比较两个数组的键值,并返回差集.大理石平台价格表 语法:array_diff(array1,array2,array3...) 参数: 参数 描述 a ...

  4. PHP array_diff() 函数

    实例 比较两个数组的值,并返回差集: <?php $a1=array("a"=>"red","b"=>"gree ...

  5. C++默认参数与函数重载 注意事项

    一.默认参数在C++中,可以为参数指定默认值.在函数调用时没有指定与形参相对应的实参时, 就自动使用默认参数. 默认参数的语法与使用:(1)在函数声明或定义时,直接对参数赋值.这就是默认参数:(2)在 ...

  6. 移动端二三事【三】:transform的矩阵(matrix)操作、transform操作函数及注意事项

    *每当在DOM浏览器中增加动态效果时,使用强大的transform和transition,总是很酸爽.抛开css,使用js操作transform还真的有点复杂,涉及到线性代数中的矩阵,但是js操作又不 ...

  7. 在ThinkPHP的common.php文件里添加公共函数的注意事项

    注意事项: 1.函数不要加public访问控制权限,因为默认就是public的. 2.当你写好了一个新函数后在本地运行发现没有问题,但是在生产环境运行会报错:找不到这个函数,解决方法是删除runtim ...

  8. 【C/C++】函数的默认参数/函数的占位参数/函数重载/注意事项

    函数的默认参数 返回值类型 函数名(参数=默认值){} #include <iostream> using namespace std; int func(int a = 10, int ...

  9. php array_intersect() 和 array_diff() 函数

    在PHP中,使用 array_intersect 求两个数组的交集比使用 array_diff 求同样两个数组的并集要快. 如果要求数组 $a 与数组 $b 的差集的个数,应该使用 count($a) ...

随机推荐

  1. SOA 面向服务架构 阅读笔记(二)

    SOA并不能保证企业的员工更加轻松,企业的收益更加客观. 6.软件组件 6.1  组件和组件的作用  通过可重用的软件代码-组件,可以构建灵活的软件. 6.2  软件组件又称为应用程序,程序,函数,模 ...

  2. MySQL-LRU_List Free_List Flush_List

    关于 LRU_List ,Free_List,Flush_List的介绍:   LRU算法:(Latest Recent Used)最近最少使用      数据库的缓冲池通过LRU算法来进行管理.   ...

  3. 1.linux源码安装nginx

    从官网下载nginx.tar.gz源码包 拷贝至Linux系统下进行解压 tar -zxvf nginx.tar.gz 进入解压后的目录,需要./configure,此步骤会报多个错,比如没有安装gc ...

  4. 基于netty的异步http请求

    package com.pt.utils; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; im ...

  5. vc 加载外部资源,释放DLL

    #include "stdafx.h"#include "resource.h" #include <Windows.h> #include < ...

  6. QT5中如何使用QFtp类

    QT5中如何使用QFtp类 http://2662597.blog.51cto.com/2652597/1279806 由于QT5对QML的支持有很大的改进,所以打算将原来基于QT4的程序移植到QT5 ...

  7. Spring boot 更改端口的几种方式

    web服务的默认端口是8080,但有时候我们需要更改成其他端口,这里有几种端口修改方式: 在application.properties文件添加配置(最简单) server.port= 在applic ...

  8. linux vim vi编辑时撤销输入操作

    linux vim vi编辑时撤销输入操作 1,esc退出输入状态 2,u 撤销上次操作 3,ctrl+r 恢复撤销

  9. NSBundle的理解和mainBundle的基本介绍

    一.NSBundle NSBundle是cocoa为bundle提供的一个类,bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像.声音.编译好的代码.nib文件.(用户也会把 ...

  10. ControlTemplate,ItemsPanelTemplate,DataTemplate(wpf)

    在WPF中有三大模板ControlTemplate,ItemsPanelTemplate,DataTemplate.其中ControlTemplate和ItemsPanelTemplate是控件模板, ...