php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22863 array_diff works differently than expected
Submitted: 2003-03-24 18:59 UTC Modified: 2003-03-24 19:07 UTC
From: pieter3d at hotmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.1 OS: WinXP
Private report: No CVE-ID: None
 [2003-03-24 18:59 UTC] pieter3d at hotmail dot com
When executing this code:

	$a = array(1,2,3,4,5,6,7,8,9);
	$b = array(3,4,5);
	$test=array_diff($a,$b);
	$i=0;
	while($test[$i])
	{
		echo "$test[$i]<br>";
		$i++;
	}

the result is:
1
2

The expected result of course is
1
2
6
7
8
9

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-24 19:04 UTC] nicos@php.net
Verified with PHP-4.3.2RC1 (UNIX and Windows NT).
 [2003-03-24 19:07 UTC] nicos@php.net
Oh well the code is bogus, actually the array returned is more like this:
array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [5]=>
  int(6)
  [6]=>
  int(7)
  [7]=>
  int(8)
  [8]=>
  int(9)
}

According to:

<?php
$a = array(1,2,3,4,5,6,7,8,9);
$b = array(3,4,5);
$test=array_diff($a,$b);
$i=0;
var_dump($test);

?>
Keys are preserved! While just stop on your scripts because key 2 isn't defined. Don't use while for this.

Not a bug, read the documentation -> Bogus.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 18:01:29 2024 UTC