php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62497 array_intersect doesn't work properly when data is reversed with duplicates
Submitted: 2012-07-06 22:30 UTC Modified: 2012-07-07 00:49 UTC
From: martin at psinas dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 5.4.4 OS: Linux
Private report: No CVE-ID: None
 [2012-07-06 22:30 UTC] martin at psinas dot com
Description:
------------
As mentioned here:
http://us3.php.net/manual/en/function.array-intersect.php#92900

array_intersect doesn't work properly when data is reversed with duplicates

Test script:
---------------
<?php
$text1 = "one two two";
$text2 = "one two three";

$old = explode(' ', $text1);
$new = explode(' ', $text2);

$preserved1 = array_intersect($old, $new);
$deleted = array_diff($old, $preserved1);
$preserved2 = array_intersect($new, $old);
$inserted = array_diff($new, $preserved2);

echo '<pre>';
print_r($preserved1);
print_r($deleted);
print_r($preserved2);
print_r($inserted);
echo '<pre>';
?>

Expected result:
----------------
Array
(
    [0] => one
    [1] => two
)
Array
(
    [2] => three
)
Array
(
    [0] => one
    [1] => two
)
Array
(
    [2] => three
)

Actual result:
--------------
Array
(
    [0] => one
    [1] => two
    [2] => two
)
Array
(
)
Array
(
    [0] => one
    [1] => two
)
Array
(
    [2] => three
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-07 00:49 UTC] martin at psinas dot com
-Status: Open +Status: Closed
 [2012-07-07 00:49 UTC] martin at psinas dot com
not a bug.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jun 05 20:01:26 2025 UTC