什么是array_diff_uassoc的实际用途? [英] What is the practical use of array_diff_uassoc?

查看:118
本文介绍了什么是array_diff_uassoc的实际用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和array_diff_assoc array_diff_uassoc 都做同样的事情(计算差值B / W和additinal指数检查数组)唯一的区别是后来的一出Accpet头一个回调

所不同的仅仅是回调,在这种情况下,你应该preFER array_diff_uassoc 而不是和array_diff_assoc

我想明白,如果回调会做以下同是每一个情况,那么有什么用 array_diff_uassoc

 函数key_compare_func($ A,$ B)
{
    如果($ A $ === B){
        返回0;
    }
    返回($ A> $ B)? 1:-1;
}


解决方案

实际的区别在于用户定义的函数可以的任何的默认值以外。的您自己定义回调。

只是因为文档只给出了一个简单的例子,并不意味着这是唯一的可能性。这里是你会使用汤姆比较多维数组元素的回调函数的一个人为的例子:

 函数key_compare_func($ A,$ B){
    如果($ A ['关键'] ['子'] === $ B ['关键'] ['子']){
        返回0;
    }
    返回($ A ['关键'] ['子'> $ B ['关键'] ['子'])? 1:-1;
}

编辑: PHP7有飞船

 函数key_compare_func($ A,$ B){
    返回$ a ['关键'] ['子'< = GT; $ B ['关键'] ['子']
}

array_diff_assoc and array_diff_uassoc both do the same thing (compute difference b/w array with additinal index check) the only difference is the later one accpet a callback.

The difference is just callback, In which case you should prefer array_diff_uassoc instead of array_diff_assoc.

I want to understand that If the callback is going to do the same as below is every case then what is the use of array_diff_uassoc

function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}

解决方案

The practical difference is that the user-defined function can be anything other than the default. You define the callback yourself.

Just because the documentation only gives a simple example doesn't mean that that is the only possibility. Here is a contrived example of a callback function you'd use tom compare elements in a multi-dimensional array:

function key_compare_func($a, $b) {
    if ($a['key']['subkey'] === $b['key']['subkey']) {
        return 0;
    }
    return ($a['key']['subkey'] > $b['key']['subkey'])? 1:-1;
}

Edit: PHP7 has spaceships!

function key_compare_func($a, $b) {
    return $a['key']['subkey'] <=> $b['key']['subkey']
}

这篇关于什么是array_diff_uassoc的实际用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆