Re: Sort an array of objects
| From: | Kevin Stone | Date: | Wed, 01 Oct 2003 21:42:21 +0000 |
| Subject: | Re: Sort an array of objects | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-164636@lists.php.net to get a copy of this message | ||
"Matt Palermo" <webmaster@sweetphp.com> wrote in message news:20031001211810.75058.qmail@pb1.pair.com...
> I have an array of object for files on my site. The objects hold a file's
> name, size, extension, etc... I was wondering if anyone knows where I could
> find a relatively easy function for sorting the array of these objects by
> either name, size, etc... Please let me know if got any tips. Thanks a
> lot!
>
> Matt
Off the top of my head..
<?
$object_array = array(
array(
'name' => 'Kevin',
'company' => 'IBM'),
array(
'name' => 'Steve',
'company' => 'Apple'),
array(
'name' => 'Bill',
'company' => 'Solaris')
);
// Store names by group index..
foreach($object_array as $i => $a) {
$sorted_array[$i] = $a['name'];
}
// Sort names and keep associated indicies..
asort($sorted_array);
// Store original groups on sorted indicies..
foreach($sorted_array as $i => $v) {
$sorted_object_array[$i] = $object_array[$i];
}
echo "<pre>";
print_r($sorted_object_array);
?>
Badda bing.
- Kevin