【问题标题】:What does array_diff_uassoc() function do in php?array_diff_uassoc() 函数在 php 中有什么作用?
【发布时间】:2017-11-05 11:26:48
【问题描述】:

我是 php 新手,我已经阅读了关于 php.netw3schools 和其他资源上的 array_diff_uassoc() 函数的文档在互联网上,但未能得到这个功能的用途。在我看来,这是无意义的功能,因为它的文档非常混乱。

据我所知,第一个参数和第二个参数是数组,但是第三个参数的作用是函数,它必须返回小于、大于或等于 0 以及 this 和 this。这个无意义的文档是什么意思?

按照所有示例生成相同的结果。

示例 1

function test($a,$b){
$a > $b ? 1 : -1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

数组 ( [二] => 元素二 [三] => 元素三 )

示例 2

function test($a,$b){
$a > $b ? -1 : 1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

数组 ( [二] => 元素二 [三] => 元素三 )

示例 3

function test($a,$b){
$a < $b ? 0 : 1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

数组 ( [二] => 元素二 [三] => 元素三 )

示例 4

function test($a,$b){
$a < $b ? 0 : 1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

数组 ( [二] => 元素二 [三] => 元素三 )

那么这个无意义的函数是什么意思呢?谁能告诉我或者可能是我错了?

【问题讨论】:

  • 您的 test 函数不符合您列出的规则。你不应该期望它有正确的结果。仅仅因为你不理解它们就小心地将它们标记为“废话”,这不是学习的最佳方式
  • 那么我应该遵循与互联网上记录的相同的文档吗?
  • 请您在单独的答案中举几个例子吗?如果清楚我的概念,我会标记你的问题。
  • 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.: 如果PHPDocs page 说用户回调函数应该返回一个值,那么你的回调函数返回一个值总是一个好主意
  • 请不要在评论中提供大纲。请用示例单独回答

标签: php arrays


【解决方案1】:

比较功能允许您创建自定义逻辑来确定两个条目是否相同。

这两个数组中的键看起来完全不同,因为它们使用不同的语言。

$data1 = [
  'red' => true,
  'yellow' => true,
  'green' => true,
  'blue' => true,
  'black' => true,
];

$data2 = [
  'rouge' => true,
  'jaune' => true,
  'bleu' => true,
  'vert' => true,
  'blanc' => true,
];

但是我们仍然可以使用自定义比较函数对它们进行比较,该函数可以识别两种语言在哪里具有等效值

function colourLanguageTest($a, $b) {
    static $comparator = [
        'red' => 'rouge',
        'yellow' => 'jaune',
        'green' => 'vert',
        'blue' => 'bleu',
        'black' => 'noir',
        'white' => 'blanc',
    ];

    if (isset($comparator[$a])) {
        return $comparator[$a] != $b;
    } elseif(isset($comparator[$b])) {
        return $comparator[$b] != $a; 
    }

    return true;
}

$result = array_diff_uassoc($data1, $data2, 'colourLanguageTest');

var_dump($result);

比较函数检查比较表中的条目,因此它可以识别redrouge 相同,并将它们视为匹配项。如果匹配则返回布尔值 false (0),如果不匹配则返回布尔值 true (1)。 因为这是一个 diff 函数,它会过滤掉我们自定义逻辑返回 0(表示匹配)的第一个数组中的所有条目,只留下我们的比较逻辑不返回 0 的条目(即返回 1 或 -1 或 999或-23456)

因为 'red'、'yellow'、'green' 和 'blue' 在第二个数组中都有对应的条目,根据语言查找匹配,只有 'black' 在第二个数据中没有对应的条目数组,所以我们调用array_diff_uassoc() 的结果返回

array(1) {
  ["black"]=>
  bool(true)
}

【讨论】:

  • 感谢您的回答。好的,如果返回 0 会发生什么,如果返回 1 会发生什么,如果返回 -1 会发生什么?如果返回 0、1 和 -1 以外的值会怎样?
  • 我只标记了你的问题,因为 0 表示匹配,1 表示在此函数的上下文中不匹配。却没有得到你。无论如何,非常感谢你。
【解决方案2】:

你的回调“测试”函数中必须有一个返回值

function test($a,$b){

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

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

【讨论】:

  • 好吧,假设函数返回 0 或 1 或 -1。那么会发生什么呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多