php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #7194 array_intersect() changes element order
Submitted: 2000-10-13 13:59 UTC Modified: 2000-10-21 14:28 UTC
From: s dot busch at billiton dot de Assigned: venaas (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.2 OS: linux (kernel 2.2.14)
Private report: No CVE-ID: None
 [2000-10-13 13:59 UTC] s dot busch at billiton dot de
The function array_intersect() does change the order of the elements
that are returned from array in first parameter. Since "order" is an essential feature of arrays it would be inconsistent to change it implicitely. Seems to be weak implementation of array_intersect().

(At least it should be documented, but i think it should be fixed)

I don't checked the other array-functions for this behavior, it could be that some show this inconsistency, too.

The resulting array seems to be in order of the contained values. In my example, the resulting array is alphabetically sorted:

---- code snippet ----
// result of array_intersect() is ex
$a = array(1=>'c',2=>'z',666=>'b',4=>'a');
$b = $a;
var_dump(array_intersect($a,$b));

---- result ----
array(4) {
  [4]=>
  string(1) "a"
  [666]=>
  string(1) "b"
  [1]=>
  string(1) "c"
  [2]=>
  string(1) "z"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-10-21 14:28 UTC] venaas@php.net
I wanted array_intersect() for "sets" where order is not important, but
I think you're right, it's good if the order is kept. I've now made both
array_intersect() and array_diff() keep the order.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC