加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP array_diff奇怪

发布时间:2020-12-13 16:01:44 所属栏目:PHP教程 来源:网络整理
导读:这是一个简单的问题,但 PHP文档并不能解释为什么会发生这种情况. 我有这个代码: var_dump($newattributes); var_dump($oldattributes);var_dump(array_diff($newattributes,$oldattributes)); 为了简单起见,我将省略我实际使用的结构的大部分(因为每个元素
这是一个简单的问题,但 PHP文档并不能解释为什么会发生这种情况.

我有这个代码:

var_dump($newattributes); var_dump($oldattributes);
var_dump(array_diff($newattributes,$oldattributes));

为了简单起见,我将省略我实际使用的结构的大部分(因为每个元素都是117个元素),而是切合实际.

我有一个名为$newattributes的数组,如下所示:

array(117){
    // Lots of other attributes here
    ["deleted"] => int(1)
}

另一个叫$oldattributes,看起来像:

array(117){
    // Lots of other attributes here
    ["deleted"] => string(1) "0"
}

哪个看起来不一样?根据array_diff:否.从array_diff得到的输出是:

array(0) { }

我已经阅读了文档页面,但是它说:

Two elements are considered equal if and only if (string) $elem1 ===
(string) $elem2. In words: when the string representation is the same.

而且我不知道“1”如何对等于“0”.

所以我看到一些关于array_diff的警告我没有考虑到

问题可能在于您正在使用关联数组:您应该尝试使用以下关联数组: array_diff_assoc():
<?php 
    $newattributes = array(
       "deleted" => 1 
    );

    $oldattributes = array(
       "deleted" => "0" 
    );

    $result = array_diff_assoc($newattributes,$oldattributes);

    var_dump($result);
?>

结果:

array(1) {
       ["deleted"]=>
       int(1)
   }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读