note 74912 deleted from function.array-multisort by cmb
| From: | cmb@php.net | Date: | Sun, 29 Sep 2019 07:42:35 +0000 |
| Subject: | note 74912 deleted from function.array-multisort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212413@lists.php.net to get a copy of this message | ||
Note Submitter: scott at bartoncomputer dot com
----
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!