array_diff_uassoc不明确的行为 [英] Behavior of array_diff_uassoc not clear

查看:167
本文介绍了array_diff_uassoc不明确的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一提的是我挖成手动和PHP文档,并没有找到答案的第一。这里有一个code我使用:

First of all I need to mention that I digged into manual and php docs and didnt find an answer. Here's a code I use:

class chomik {

    public $state = 'normal';
    public $name = 'no name';

    public function __construct($name) {
        $this->name = $name;
    }

    public function __toString() {
        return $this->name . " - " . $this->state;
    }
}

function compare($a, $b) {
    echo("$a : $b<br/>");
    if($a != $b) {
        return 0;
    }
    else return 1;
}

$chomik = new chomik('a');
$a = array(5, $chomik, $chomik, $chomik);
$b = array($chomik, 'b', 'c', 'd');
array_diff_uassoc($a, $b, 'compare');

我在想什么,array_diff_uassoc将比较这两个数组的所有值,如果值存在,那么将运行键比较。而这code的输出是:

What I thought, array_diff_uassoc will compare all values of these two arrays, and if values exists, then will run key comparison. And the output of this code is:

1 : 0
3 : 1
2 : 1
3 : 2
1 : 0
3 : 1
2 : 1
3 : 2
3 : 3
3 : 2
2 : 3
1 : 3
0 : 3

所以首先为什么有些对(1:0或3:1)被复制?这是否意味着功能忘了,它已经比这个项目?我认为这会比较都等于按值对,但我没有看到它的输出。我缺少的东西吗?

So first of all why some pairs (1 : 0 or 3 : 1) are duplicated? Does it mean function forgot that it already compared this items? I thought that it will compare all equal-by-value pairs, but I dont see it in output. Am I missing something?

所以问题是:什么是比较顺序而言该功能的具体的行为,为什么我看到这个重复? (我的PHP版本,如果它可以帮助是:PHP版本5.3.6-13ubuntu3.6)

So question is: what is exact behavior of this function in terms of order of comparison, and why I see this duplicates? (my PHP version, if it helps is: PHP Version 5.3.6-13ubuntu3.6)

我真的很困惑,并等待它的一些很好的解释...

I'm really confused, and waiting for some good explanation of it...

推荐答案

href=\"http://stackoverflow.com/users/571230/carlos\">运的 <一个href=\"http://stackoverflow.com/questions/9458652/behavior-of-array-diff-uassoc-not-clear/29421428#comment12142873_9458652\">comment这

from op's comment that

欲仅这些要素,不能在第二阵列($一个[0])

I want only these elements which are not in second array ($a[0])

你不能使用和array_diff($ A,$ B); ?它返回

array(1) {
  [0]=>
  int(5)
}


,否则

指出该文档:

如果第一个参数被认为是<强>分别小于,等于,或大于第二更大的比较函数必须返回小于一个整数,等于或大于零。

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

据我了解,这意味着比较()功能应该更是这样的:

As I understand it, that means that the compare() function should be more like this:

function compare($a, $b) {
    echo("$a : $b<br/>");
    if($a === $b) return 0;
    else if ($a > $b) return 1;
    else return -1;
}

然而,即使有这种修正,它很奇怪结果进行比较:

However even with this correction, it has very strange compare results:


1 : 0
1 : 2
3 : 1
2 : 1
3 : 2
1 : 0
1 : 2
3 : 1
2 : 1
3 : 2
0 : 0
1 : 0
1 : 1
2 : 0
2 : 1
2 : 2
3 : 0
3 : 1
3 : 2
3 : 3

我问另一个问题关于这个问题,有人走出一个答案的范围。

I asked another question about this as it was getting out of the scope of an answer.

这篇关于array_diff_uassoc不明确的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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