note 74912 added to function.array-multisort
| From: | scott at bartoncomputer dot com | Date: | Thu, 03 May 2007 17:18:07 +0000 |
| Subject: | note 74912 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126154@lists.php.net to get a copy of this message | ||
I didn't see this noted anywhere, so I figured I'd put in a little comment regarding
arrays located inside classes. For instance:
class abc
{
var $data;
}
The following code does not act as expected:
$clsVar =& new abc();
foreach ($clsVar->data as $key => $row)
{
$position[$key] = $key;
}
array_multisort($position, SORT_DESC, $clsVar->data);
While I realize this could much easily be acheived using ksort(), this is merely a much more simple
example of this behaviour. The exerpt above comes from a much more complicated sort using
multi-scripted arrays.
Anyway the only way I could find to get around the behaviour of multisort not sorting the referenced
class-array was to make a copy of it as below:
$clsVar =& new abc();
$newData = $clsVar->data;
foreach ($newData as $key => $row)
{
$position[$key] = $key;
}
array_multisort($position, SORT_DESC, $newData);
Now newData will contain the sorted array as expected.
Hopefully this helps someone else!
----
Server IP: 64.71.164.2
Probable Submitter: 12.38.98.215
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/74912
Del: integrated -- https://master.php.net/note/delete/74912/integrated
Del: useless -- https://master.php.net/note/delete/74912/useless
Del: bad code -- https://master.php.net/note/delete/74912/bad+code
Del: spam -- https://master.php.net/note/delete/74912/spam
Del: non-english -- https://master.php.net/note/delete/74912/non-english
Del: in docs -- https://master.php.net/note/delete/74912/in+docs
Del: other reasons-- https://master.php.net/note/delete/74912
Reject -- https://master.php.net/note/reject/74912
Search -- https://master.php.net/manage/user-notes.php