php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68282 Array resulting from array_diff starts at wrong index
Submitted: 2014-10-22 06:15 UTC Modified: 2014-10-22 13:07 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: david dot lin dot au at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.34 OS:
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: david dot lin dot au at gmail dot com
New email:
PHP Version: OS:

 

 [2014-10-22 06:15 UTC] david dot lin dot au at gmail dot com
Description:
------------
Adding elements to array resulting from array_diff is allocated with wrong numeric index key. 

Test script:
---------------
<?php

$a1 = array(
    'one',
    'two',
    'three',
    'four'
);
$a2 = array_diff(
    $a1,
    array(
        'one',
        'two',
        'three',
        'four'
    )
);

$a3 = $a2;
$a3[] = 'five';

print_r($a3);

Expected result:
----------------
Array
(
    [0] => five
)

Actual result:
--------------
Output for PHP 4.3.0 - 5.4.32, 5.5.0 - 5.5.16, 5.6.0, php7@20140507 - 20140521, php7@20140801, hhvm-3.0.1 - 3.3.0 :

Array
(
    [0] => five
)

Output for PHP 5.4.34, 5.5.18, 5.6.1 - 5.6.2, php7@20140701, php7@20140901 - 20141001 :

Array
(
    [4] => five
)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-22 13:07 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2014-10-22 13:07 UTC] bwoebi@php.net
What had been changed by http://git.php.net/?p=php-src.git;a=commitdiff;h=99f0760bfba86f45af9ea011a0f017080922710a is that next array keys to use aren't reset anymore if an array is copied. (That it wasn't copied *was* a bug)

Also, array_diff just copies the next key to use from the first array. So, the next key to use in $a1 was 4, in $a2 then too. Which is copied also to $a3; that's why you get key 4 when you try to $a3[] = "value";.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 12:01:33 2025 UTC