【问题标题】:array_diff() function not showing correct resultarray_diff() 函数未显示正确结果
【发布时间】:2016-10-04 06:17:58
【问题描述】:

array_diff() 函数未显示正确结果:-

第一个数组:-

     Array(
    [designation_id] => 1
    [name] => Y
    [fathers_name] => Z
    [mothers_name] => F
    [spouse_name] => d
    [gender] => F
    [last_achieved_degree] => 2
    [date_of_birth] => 1960-10-17
    [date_of_joining] => 1987-02-04
)

第二个数组:-

 Array
    (
        [designation_id] => 9
        [name] => M
        [fathers_name] => N
        [mothers_name] => O
        [spouse_name] => 
        [gender] => M
        [last_achieved_degree] => 1
        [date_of_birth] => 1967-11-17
        [date_of_joining] => 2016-01-01
    )

输出:

    Array
(
    [name] => Y
    [fathers_name] => Z
    [mothers_name] => F
    [spouse_name] => d
    [gender] => F
    [last_achieved_degree] => 2
    [date_of_birth] => 1960-10-17
    [date_of_joining] => 1987-02-04
)

指定 ID 列未显示在输出结果中。 如果 designation-id 值为 1,则此索引不显示在输出中,否则显示。是bug还是别的什么?

【问题讨论】:

  • 为什么不显示??分享你的原始脚本。
  • 你显示的输入是两个不同的数组?
  • 那么正确的输出应该是什么?也许array_diff 并不是你真正需要的。你的试验代码在哪里?
  • 用户 array_diff_assoc() 而不是 array_diff()

标签: php array-difference


【解决方案1】:

由于你的数组是关联数组,所以你需要使用array_diff_assoc:-

<?php

 $a =   Array(
    'designation_id' =>1,
    'name' => 'Y',
    'fathers_name' => 'Z',
    'mothers_name' => 'F',
    'spouse_name' => 'd',
    'gender' => 'F',
    'last_achieved_degree' => 2,
    'date_of_birth' => '1960-10-17',
    'date_of_joining' => '1987-02-04'
);

$b =Array
(
    'designation_id' => 9,
    'name' => 'M',
    'fathers_name' => 'N',
    'mothers_name' => 'O',
    'spouse_name' => '',
    'gender' => 'M',
    'last_achieved_degree' => 1,
    'date_of_birth' => '1967-11-17',
    'date_of_joining' => '2016-01-01',
);

echo "<pre/>";print_r(array_diff_assoc($a,$b));

输出:- https://3v4l.org/NKbuX

查看更多描述和示例:- http://sg2.php.net/manual/en/function.array-diff-assoc.php

为什么array_diff() 不起作用:- https://stackoverflow.com/a/4742438/4248328

【讨论】:

    【解决方案2】:

    array_diff 在不比较键的情况下计算差异。在第二个数组中,您有 [last_achieved_degree] =&gt; 1 删除 [designation_id] =&gt; 1
    试试array_diff_assoc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 2022-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多