Rumah > pembangunan bahagian belakang > tutorial php > array_diff函数的注意事项,array_diff函数_PHP教程

array_diff函数的注意事项,array_diff函数_PHP教程

WBOY
Lepaskan: 2016-07-12 09:03:32
asal
841 orang telah melayarinya

array_diff函数的注意事项,array_diff函数

  array_diff — 计算数组的差集

  说明:

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

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

  

<?<span>php
</span><span>header</span>("Content-type:text/html;charset=utf-8"<span>);
</span><span>$json1</span>='{ "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" } } ] } ] }'<span>;

</span><span>$json2</span>='{ "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" } } ] } ] }'<span>;

</span><span>$array1</span>=json_decode(<span>$json1</span>,<span>true</span><span>);
</span><span>$array2</span>=json_decode(<span>$json2</span>,<span>true</span><span>);


</span><span>function</span> array_recursive_diff(<span>$array1</span>, <span>$array2</span><span>) {
    </span><span>$result</span> = <span>array</span><span>();
    </span><span>foreach</span> (<span>$array1</span> <span>as</span> <span>$key1</span> => <span>$value1</span><span>) {
        </span><span>if</span> (<span>array_key_exists</span>(<span>$key1</span>, <span>$array2</span><span>)) {
            </span><span>if</span> (<span>is_array</span>(<span>$value1</span><span>)) {
                </span><span>$diff</span> = array_recursive_diff(<span>$value1</span>, <span>$array2</span>[<span>$key1</span><span>]);
                </span><span>if</span> (<span>count</span>(<span>$diff</span><span>)) {
                    </span><span>$result</span>[<span>$key1</span>] = <span>$diff</span><span>;
                }
            } </span><span>else</span><span> {
                </span><span>if</span> (<span>$value1</span> != <span>$array2</span>[<span>$key1</span><span>]) {
                    </span><span>$result</span>[<span>$key1</span>] = <span>$value1</span><span>;
                }
            }
        } </span><span>else</span><span> {
            </span><span>$result</span>[<span>$key1</span>] = <span>$value1</span><span>;
        }
    }

    </span><span>return</span> <span>$result</span><span>;
}

</span><span>$result</span>=array_recursive_diff(<span>$array1</span>, <span>$array2</span><span>);
</span><span>echo</span> '<pre class="brush:php;toolbar:false">'<span>;
</span><span>var_dump</span>(<span>$result</span><span>);

</span><span>if</span>(<span>empty</span>(<span>$result</span><span>)){
    </span><span>echo</span> '完全相同'<span>;
}</span><span>else</span><span>{
    </span><span>echo</span> '完全不相同'<span>;
}</span>
Salin selepas log masuk

 

  

  

  

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1080757.htmlTechArticlearray_diff函数的注意事项,array_diff函数 array_diff 计算数组的差集 说明: array array_diff ( array $array1 , array $array2 [, array $... ] ) 对比返回在 arr...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan