php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60932 array_diff_assoc not working as expected.
Submitted: 2012-01-30 07:06 UTC Modified: 2012-01-30 11:29 UTC
From: rupertrutland at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.9 OS: MAC OSX 10.6.8
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rupertrutland at gmail dot com
New email:
PHP Version: OS:

 

 [2012-01-30 07:06 UTC] rupertrutland at gmail dot com
Description:
------------
The first key/value pair should, in theory, not be present in the result array.

Test script:
---------------
$arrayAssoc1 = array(
	'one' => '1: some val',
	'two' => '1: another val',
	'three' => '1: and another val',
	'four' => '1: fourth val',
	'five' => '1: fifth val',
	'six' => '1: sixth param',
	'seven' => '1: sixth param',
	'starwars' => '2: lightsaber'
);
$arrayAssoc2 = array(
	'one' => '1: some val',
	'two' => '1: another val',
	'three' => '1: and another val',
	'one' => '2: some val',
	'five' => '1: fifth val',
	'three' => '2: and another val',
	'seven' => '2: and another val',
	'starwars' => '2: lightsaber'
);
var_dump(array_diff_assoc($arrayAssoc1, $arrayAssoc2));

Expected result:
----------------
array(5) {
  ["three"] => string(18) "1: and another val"
  ["four"] => string(13) "1: fourth val"
  ["six"] => string(14) "1: sixth param"
  ["seven"] => string(14) "1: sixth param"
}

Actual result:
--------------
array(5) {
  ["one"] => string(11) "1: some val"
  ["three"] => string(18) "1: and another val"
  ["four"] => string(13) "1: fourth val"
  ["six"] => string(14) "1: sixth param"
  ["seven"] => string(14) "1: sixth param"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-30 11:29 UTC] johannes@php.net
The issue is in your array definition:

$arrayAssoc2 = array(
	'one' => '1: some val',
/*...*/
	'one' => '2: some val',
/*...*/
);

so you're overwriting the key 'one' again. If you replace the second 'one' with 'four' it works as you expect.
 [2012-01-30 11:29 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 07:01:32 2025 UTC