php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33887 array_diff returns empty in some cases
Submitted: 2005-07-27 17:35 UTC Modified: 2005-08-04 01:00 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: jestrada at webmedia dot com dot mx Assigned:
Status: No Feedback Package: Arrays related
PHP Version: 5.0.4 OS: Win
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: jestrada at webmedia dot com dot mx
New email:
PHP Version: OS:

 

 [2005-07-27 17:35 UTC] jestrada at webmedia dot com dot mx
Description:
------------
the default function array_diff returns empty records in some cases. i have two arrays

1) (1),(3),(7),(9)
2) (2),(3)

it must return r) (1), (7), (9), but it returns (), (), (9)

i made a similar function by my self, and i had the same problem, then i review the code, and i found a line, a simple counter:

    if ($l_existe == false){
      <b>$arr_result[$i]</b> = $st_valorComparado;
      $i_result++;
    }

the var $i must be $i_result, that's why the array contains in somecases, in which the number of found elements is less than the max elements, the empty recs, because $arr_result[$i] is putting the value of $st_valorComparado in another index higher than the max of elements found.

Maybe the code of the original funcion have a similar error.


Reproduce code:
---------------
function fcn_array_diff($arr_comparado, $arr_compararEn){
  $i_maxComparado = count($arr_comparado);
  $i_maxCompararEn = count($arr_compararEn);
  $arr_result[0] = "";
  $i_result = 0;

  for ($i=0; $i<$i_maxComparado; $i++){
    $st_valorComparado = chop($arr_comparado[$i]);
    $l_existe = false;

    for ($j=0; $j<$i_maxCompararEn; $j++){
      $st_valorCompararEn = chop($arr_compararEn[$j]);

      if ($st_valorComparado == $st_valorCompararEn){
        $l_existe = true;
        break;
      }
    }

    <b>if ($l_existe == false){
      $arr_result[$i_result] = $st_valorComparado;
      $i_result++;
    }</b>
  }

  return $arr_result;
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-27 17:40 UTC] tony2001@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc.

If possible, make the script source available online and provide
an URL to it here. Try to avoid embedding huge scripts into the report.

I don't see any calls to array_diff() in your code.
 [2005-08-04 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-07-22 04:47 UTC] ru61 at bk dot ru
I have this problem too...
php 4.4.0, freebsd
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 10:01:31 2024 UTC