php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45708 uasort overwrite bug
Submitted: 2008-08-04 13:42 UTC Modified: 2008-08-04 14:19 UTC
From: watermark86 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: ubuntu 8.04
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: watermark86 at gmail dot com
New email:
PHP Version: OS:

 

 [2008-08-04 13:42 UTC] watermark86 at gmail dot com
Description:
------------
uasort overwrites the last element in a large associative array with incrementing numbers

Reproduce code:
---------------
$detaildata = array();
for($i = 0; $i < 6; $i++) {
	$lid = rand(1,200);
	$numpeople = rand(0,12);
	
	for($p = 0; $p < $numpeople; $p++) {
		$pid = rand(1,200);
		$detaildata[$lid][$pid] = array("string data","string data",$pid);
	}
}
$detaildata[9][44] = array("string data","string data",44);
//
foreach($detaildata as &$row) {
	uasort($row,CalSortCallback);
}
//
echo is_array($detaildata[9])?'yes':'no';
for($row = 0; $row < 5; $row++) {
	echo is_array($detaildata[9])?'yes':$detaildata[9];
}
//
function CalSortCallback($a,$b) {
	return strcmp($a[1],$b[1]);
}

Expected result:
----------------
it should print 6 yeses

yesyesyesyesyesyes

Actual result:
--------------
it prints yes01234

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-04 14:19 UTC] jani@php.net
Quite expected: $row is still reference to that one entry. You modify it to 0 in the for() clause..
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 01:01:28 2024 UTC