php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18315 Different behaviour of array_diff in a variables and encapsuled with each
Submitted: 2002-07-12 10:39 UTC Modified: 2002-07-12 14:11 UTC
From: cirello at estadao dot com dot br Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.2.1 OS: Linux Red Hat Linux release 7.1
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cirello at estadao dot com dot br
New email:
PHP Version: OS:

 

 [2002-07-12 10:39 UTC] cirello at estadao dot com dot br
/* Two arrays do diff */
$array_one = Array(1,2,3);
$array_two = Array(1,2);

/* Bugged behaviour*/
while( list(,$value) = @each(array_diff($array_one, $array_two)) )
         echo $value."\n";
/* But this while loop never ends ... If the array_diff returns just one value , why is it never ending?*/

/* WorkAround */
$array_diffed = array_diff($array_one, $array_two);
while( list(,$value) = @each($array_diffed) )
         echo $value."\n";

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-12 13:01 UTC] cynic@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2002-07-12 13:03 UTC] cynic@php.net
one more note: array_diff() is not a generator, and it's run as many times
as it returns non-false, IOW in this case forever.

 [2002-07-12 13:09 UTC] cirello at estadao dot com dot br
OK, if it is not a bug on array_diff there must be some problem somewhere.

A deeper look on the possibilities I understand that the problem might be layed on each function.

Anyway I shall discuss it on newsgroup first... Then I'll apply for another bug check or not.

Thank you.

Carlos Cirello
 [2002-07-12 13:40 UTC] cynic@php.net
ok, I'll explain it once more:

$a = array(1, 2, 3);
$b = array(1, 2);

while (list(,$v) = each(array_diff($a, $b)));

let's dissect this a bit:

1. invocation:
array_diff() is called, returning array(3)
each() is called, returning array(0, 3)
list() is called, creating $v (3)

2. invocation
array_diff() is called, returning array(3)
each() is called, returning array(0, 3)
list() is called, creating $v (3)

repeat ad nauseam

happy now?


 [2002-07-12 14:11 UTC] cirello at estadao dot com dot br
oh now I see...
thank you!
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Aug 03 10:01:24 2020 UTC