array_diff函數的注意事項

來源:互聯網
上載者:User
array_diff — 計算數組的差集

  說明:

array array_diff ( array $array1 , array $array2 [, array $... ] )  對比返回在 array1 中但是不在 array2 及任何其它參數數組中的值。注意鍵名保留不變。

  注意:本函數只檢查了多維陣列中的一維。如果想比較更深的維度需要另寫一個函數,今天的工作就遇到了這樣的需求,所以寫了一個函數來比較更深的維度。

phpheader("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 ($array1as$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 '
';var_dump($result);if(empty($result)){    echo '完全相同';}else{    echo '完全不相同';}

以上就介紹了array_diff函數的注意事項,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.