A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearSorting an Array of Objects in PHP In a Specific Order - page 1
User InfoPosts
Sorting an Array of Objects in PHP In a Specific Order#1
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids in a particular order, like so: (8, 1, 6)

The second array ($user_results) is comprised of an array of objects like so:

Array
(
[0] => stdClass Object
(
[ID] => 1
[user_login] => user1
)
[1] => stdClass Object
(
[ID] => 6
[user_login] => user6
)
[2] => stdClass Object
(
[ID] => 8
[user_login] => user8
)
)


I'd like to "sort" the second array so it's in this order, which matches the order of the values in the first array of (8, 1, 6). So it'd look like this:

Array
(
[0] => stdClass Object
(
[ID] => 8
[user_login] => user8
)
[1] => stdClass Object
(
[ID] => 1
[user_login] => user1
)
[2] => stdClass Object
(
[ID] => 6
[user_login] => user6
)
)


I'm weak on data structures. How could I do this? :-)

Thanks in advance for your help!

-Bob

posted date: 2008-12-10 01:53:00


Re: Sorting an Array of Objects in PHP In a Specific Order#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-10 01:53:01


Re: Sorting an Array of Objects in PHP In a Specific Order#3
Use usort and provide a custom comparison function which uses the position of the key in your "ordering" array to determine the sort order, e.g. something like:function cmp($a, $b) { global $author_array; $pos1=array_search ($a->ID, $author_array); $pos2=array_search ($b->ID, $author_array); if ($pos1==$pos2) return 0; else return ($pos1 < $pos2 ? -1 : 1);}usort($user_results, "cmp");

posted date: 2008-12-10 02:06:00


Re: Sorting an Array of Objects in PHP In a Specific Order#4
I'm not sure whether this will be significantly slower than other examples, but it seems simpler. It may be that you could build the $user_results array as an associative one to start with, using ID as the key, then you can easily do lookups.$hash = array();$result = array();foreach ($user_results as $obj) { $hash[$obj->ID] = $obj;}foreach ($author_array as $id) { $result[] = $hash[$id];}

posted date: 2008-12-10 02:11:00


Re: Sorting an Array of Objects in PHP In a Specific Order#5
Why the -1? It demonstrates a valid technique the OP might not have been aware of, and it different to the other answers as it performs the sort "in place" rather than making a copy of the original array.

posted date: 2008-12-10 03:13:00


Re: Sorting an Array of Objects in PHP In a Specific Order#6
Yes, who in their right mind downvotes a usort() solution when the issue is custom ordering? :| +1

posted date: 2008-12-10 03:16:00


Re: Sorting an Array of Objects in PHP In a Specific Order#7
This is the solution that the PHP language developers recommend.

posted date: 2008-12-10 03:28:00


Re: Sorting an Array of Objects in PHP In a Specific Order#8
This worked beautifully when dropped into my code. I will now endeavor to understand how the heck it works! :-)

posted date: 2008-12-10 18:47:00


Re: Sorting an Array of Objects in PHP In a Specific Order#9
So I had a similar issue, but I was trying to order an object by an array (that wasn't an object). It's just not possible, so here's my workaround:$sort_array= array(11, 4, 16, 19, 23);$myobject = sort_categories($myobject)function cmp($a, $b) { if ($a->sort_key == $b->sort_key) { return 0; } return ($a->sort_key < $b->sort_key) ? -1 : 1;}function sort_categories($obj) { global $sort_array; foreach($obj as $cat) { $cat->sort_key = 999; for ($i=0;$i<count($sort_array);$i++) { if ($sort_array[$i] == $cat->term_id) $cat->sort_key = $i; } } usort($obj,'cmp'); return $obj;}I'm looking through the object and adding a new (property?) called "sort_key" which we'll then use to sort on with usort() and cmp(). By default, the new sort_key will be 999 so it gets stuck at the end.

posted date: 2011-02-26 14:09:00


Re: Sorting an Array of Objects in PHP In a Specific Order#10
public static function reorganizeBykey ($objects, array $keys){ $results = array(); foreach($keys as $key){ $i=0; foreach($objects as $object){ if($object->sourceName==$key){ $results[$i] = $object; } $i++; } } $others = (array_diff_assoc($objects,$results)); $results = array_merge($results,$others); return $results;}I hope this is helpful – I had to use this to sort an array() by some keys() and then append what doesn't match at the end.

posted date: 2012-04-20 14:15:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.