php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6002 array_diff / array_intersect returning wrong elements
Submitted: 2000-08-07 06:25 UTC Modified: 2000-08-12 01:44 UTC
From: hpdl at theexchangeproject dot org Assigned: venaas (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.1pl2 OS: FreeBSD 4.0
Private report: No CVE-ID: None
 [2000-08-07 06:25 UTC] hpdl at theexchangeproject dot org
array_diff was introduced in 4.0.1, but i cant get it to return the elements right.

$array1 = array ("page1", "page2", "page3", "page4");
$array2 = array ("page1", "page4");               
$result = array_diff ($array1, $array2);         

for ($i=0; $i < sizeof($result); $i++) {
  echo $i . '-' . $result[$i] . '<br>' . "\n";
}

i am looking for a result of array('page2','page3') - but gives me array('','page2')

array_intersect returns array('page1') with the above example.. is it not meant to return array('page1','page4') ?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-07 07:57 UTC] waldschrott@php.net
both: array_diff() && array_intersect:

not really sure whether it?s a documentation issue (unexpected behaviour) or a bug, if you try
"var_dump($result);" you?ll receive 
array(2) { [1]=> string(5) "page2" [2]=> string(5) "page3" } 
that?s fine, it has preserved the keys too! (perhaps a nice feature)
I think it?s like with comparing arrays with == and ===, I?d vote for a FLAG on array_diff() (and array_intersect()), indicating if it should perform an ordered _diff (_intersect)(preserving keys, usually equal or fewer matches then unordered _diff) or unordered _diff (_intersect)(flatten(resuling array begins at index 0), finds all similarities)

Resum?: I think it should be transformed into a Feature/Request bug and if not changed, it should be documented.
 [2000-08-12 01:44 UTC] venaas@php.net
As said in the previous comment, this is because keys/indices are
preserved, which might be useful in some cases. This should have
been documented, it is now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 19:01:31 2024 UTC