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

时间:2016-10-04 06:17:59

标签: php array-difference

array_diff()功能无法显示正确的结果: -

First Array: -

     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列。 如果specified-id值为1,则此索引不显示在输出中,否则显示。是bug还是别的什么?

2 个答案:

答案 0 :(得分:2)

由于您的数组是关联数组,因此您需要使用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://eval.in/654658

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

为什么array_diff()无效: - https://stackoverflow.com/a/4742438/4248328

答案 1 :(得分:1)

array_diff计算差异而不比较键。在第二个数组中,您[last_achieved_degree] => 1删除了[designation_id] => 1 试试array_diff_assoc