php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48492 array_intersect() result shuffle
Submitted: 2009-06-08 08:02 UTC Modified: 2009-06-18 01:00 UTC
From: alex at emailclick dot com Assigned:
Status: No Feedback Package: Arrays related
PHP Version: 5.2.9 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alex at emailclick dot com
New email:
PHP Version: OS:

 

 [2009-06-08 08:02 UTC] alex at emailclick dot com
Description:
------------
There is a strange behaviour when I try to check the intersection between two array (of keys (of filtered by callback arrays)).
The result of that operation is saved in an array which contains the value at index 0 when the cycle is odd (1,3,5...). Instead the value will be saved at index 1 when the cycle is pair (2,4,6...).

Reproduce code:
---------------
foreach($dates as $date){
foreach($machines as $model){
		if(!is_array($models[$model])) models[$model]=array();
				$id=array_intersect(					array_keys(array_filter($data->data['udate'],create_function('$date','return $date=="'.$date.'";'))),array_keys(array_filter($data->data['model'],create_function('$model','return $model=="'.$model.'";'))));
		var_dump($id);echo '<br />';
	}
}

Expected result:
----------------
array(1) { [0]=>  int(0) }
array(1) { [0]=> int(1) }
array(1) { [0]=> int(2) }
array(1) { [0]=> int(3) } 

Actual result:
--------------
array(1) { [0]=>  int(0) }
array(1) { [1]=> int(1) }
array(1) { [0]=> int(2) }
array(1) { [1]=> int(3) } 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-08 08:20 UTC] alex at emailclick dot com
It seems that the problem concerns the second cycle: if there are 3 models instead 2 the array that contains the result switchs its position complying to the time of the cycle, so when we use 3 models the results are consequentially saved in the indexes 0, 1, 2, 0, 1, 2 etc..
 [2009-06-10 12:51 UTC] jani@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 the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2009-06-10 13:19 UTC] alex at emailclick dot com
This code can generate the "bug".
<?php
$dates=array("2009-06-09 16:27:00","2009-06-10 15:04:54");
$machines=array("A","B","C");

$data['udate']=array("2009-06-09 16:27:00","2009-06-09 16:27:00","2009-06-09 16:27:00","2009-06-10 15:04:54","2009-06-10 15:04:54","2009-06-10 15:04:54");
$data['model']=array("A","B","C","A","B","C");

$models=array();

foreach($dates as $date){
	foreach($machines as $model){
		if(!is_array($models[$model])) $models[$model]=array();
		$id=array_intersect(
			array_keys(array_filter($data['udate'],create_function('$date','return$date=="'.$date.'";'))),
			array_keys(array_filter($data['model'],create_function('$model','return $model=="'.$model.'";')))
		);
		var_dump($id);echo '<br />';
	}
}
?>
 [2009-06-18 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".
 [2009-06-19 14:13 UTC] alex at emailclick dot com
I've forgotten the password, need to repost?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 07 09:01:28 2024 UTC