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