php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32031 Using array_multisort on a copy of an array affects the original
Submitted: 2005-02-20 04:26 UTC Modified: 2005-02-21 18:48 UTC
From: webmaster at korfballworld dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.10 OS: MacOS X, Linux
Private report: No CVE-ID: None
 [2005-02-20 04:26 UTC] webmaster at korfballworld dot com
Description:
------------
I have a class function that uses an internal array. 
When making a copy of this array and performing 
array_multisort on the copy of the array, using the 
original array lateron in the class results in an array, 
hinting the original array has been affected, UNLESS I 
give a statement involving the original array.

See the code sample.

Adding a line where the key of one element of the 
original array is printed before the array_multisort is 
called results in the original array keeping its 
original keys, where otherwise the key are reordered as 
if the original array was used in the array_multisort. 
This was testes by printing a key only after calling the 
array_multisort as well as both before and after.

(It seems to be similar to an old bug #8130 which was 
closed, but I feel this to be such a fundemental error 
that I post this one anyway. To me this messes up 
'sensible' programming. I couldn't explain this to any 
serious programmer. It's like working on a reference to 
a variable instead of the new variable it's supposed to 
be.)

Reproduce code:
---------------
$teams_list = $this->_teams;
# This line is to fix a weird bug regarding the array_multisort screwing up the original array, despite using the copy $teams_list otherwise
print "<!-- ".key($this->_teams)." -->";
array_multisort($teams, SORT_ASC, SORT_STRING, $teams_list);

Full class copied for viewing on:
http://korfball.kicks-ass.org/assets/classes/League.inc.php.txt

Page using the class:
http://korfball.kicks-ass.org/leagues/00072/
I can 'turn on' the error when needed

Expected result:
----------------
I expect to have only the array $teams_list to be 
affected by the array_multisort, so using the original 
$this->_teams later in the class will still work the 
same.

Actual result:
--------------
Removing the print line with key($this->_teams) will 
result in an 'Fatal error: Call to a member function on 
a non-object' later in the class where $this->_teams is 
used.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-21 18:38 UTC] tony2001@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See #25359.
 [2005-02-21 18:48 UTC] webmaster at korfballworld dot com
Sorry, but the bug # you mention clearly does NOT relate 
to my problem!!! The array_multisort DOES work, it just 
affects the original array of the copy used in the 
array_multisort!!! Please read carefully before calling 
my entry 'Bogus'!! :-(

As I wrote before it seems to be similar to the very old 
bug # 8130!
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu May 23 14:01:34 2019 UTC