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 '完全不相同';
}
如果您阅读过此文章有所收获,请为我顶一个,如果文章中有错误的地方,欢迎指出。
相互学习,共同进步!
array_diff函数的注意事项的更多相关文章
- Inline函数使用注意事项
Inline函数使用注意事项 1.在一个文件中定义的inline函数不能再另一个文件中使用 2.inline函数应简洁,只有少数几个语句. 3.在inline函数中不能有循环,if,switch语句. ...
- php比较两个数组的差异array_diff()函数
下面简单介绍php比较两个数组的差异array_diff()函数. 原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.php/archives/ ...
- php array_diff()函数 语法
php array_diff()函数 语法 作用:比较两个数组的键值,并返回差集.大理石平台价格表 语法:array_diff(array1,array2,array3...) 参数: 参数 描述 a ...
- PHP array_diff() 函数
实例 比较两个数组的值,并返回差集: <?php $a1=array("a"=>"red","b"=>"gree ...
- C++默认参数与函数重载 注意事项
一.默认参数在C++中,可以为参数指定默认值.在函数调用时没有指定与形参相对应的实参时, 就自动使用默认参数. 默认参数的语法与使用:(1)在函数声明或定义时,直接对参数赋值.这就是默认参数:(2)在 ...
- 移动端二三事【三】:transform的矩阵(matrix)操作、transform操作函数及注意事项
*每当在DOM浏览器中增加动态效果时,使用强大的transform和transition,总是很酸爽.抛开css,使用js操作transform还真的有点复杂,涉及到线性代数中的矩阵,但是js操作又不 ...
- 在ThinkPHP的common.php文件里添加公共函数的注意事项
注意事项: 1.函数不要加public访问控制权限,因为默认就是public的. 2.当你写好了一个新函数后在本地运行发现没有问题,但是在生产环境运行会报错:找不到这个函数,解决方法是删除runtim ...
- 【C/C++】函数的默认参数/函数的占位参数/函数重载/注意事项
函数的默认参数 返回值类型 函数名(参数=默认值){} #include <iostream> using namespace std; int func(int a = 10, int ...
- php array_intersect() 和 array_diff() 函数
在PHP中,使用 array_intersect 求两个数组的交集比使用 array_diff 求同样两个数组的并集要快. 如果要求数组 $a 与数组 $b 的差集的个数,应该使用 count($a) ...
随机推荐
- css li 间隙
如果 li 未浮动,而 li 子元素浮动,则ie6和ie7下会出现间隙,解决办法是给 li 写上css hack *vertical-align:bottom;
- 自己的第一个MapReduce程序
数据源:来自互联网招聘hadoop岗位的薪资数据,其中几行示例数据如下: 美团 3-5年经验 15-30k 北京 [够牛就来]hadoop高级工程... 北信源 3-5年经验 15-20k 北京 Ja ...
- Java 封装、继承、多态
Java中使用 extends 关键字 进行父类继承 在初始化子类时,子类会自动执行父类的构造方法, 如果子类的构造方法中没有显示调用父类的构造方法, 则系统会默认调用父类无参的构造方法 super( ...
- Centos6.5安装glusterfs3.6.2
硬件环境Centos6.5 glusterfs3.6.2 先安装必要的包 yum install flex bison 2. 下载glusterfs3.6.2 wget http://downloa ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- 直播P2P技术3-伙伴节点质量评估及子流订阅
以上模型,暂且称之为W-P2P吧.
- odl v2 driver
networking-odl项目的目的/用途就是sync odl和neutron的资源数据库和状态 v1中对于每个neutron的资源操作都相应的调用odl restfu api来同步odl,但问题有 ...
- 新建Maven项目Index.jsp文件报错解决方法
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path in ...
- JavaScriptSerializer 日期处理 JSON.Net
[WebMethod(Description = "取得所有人员 自带json")] [SoapHeader("key")] [ScriptMethod(Res ...