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 yearSort an Array by keys based on another Array? - page 1
User InfoPosts
Sort an Array by keys based on another Array?#1
Is it possible in PHP to do something like this? How would you go about writing a function? Here is an example. The order is the most important thing.

$customer['address'] = '123 fake st';
$customer['name'] = 'Tim';
$customer['dob'] = '12/08/1986';
$customer['dontSortMe'] = 'this value doesnt need to be sorted';


And I'd like to do something like

$properOrderedArray = sortArrayByArray($customer, array('name', 'dob', 'address'));


Because at the end I use a foreach() and they're not in the right order (because I append the values to a string which needs to be in the correct order and I don't know in advance all of the array keys/values).

I've looked through PHP's internal array functions but it seems you can only sort alphabetically or numerically.

posted date: 2008-12-07 16:24:00


Re: Sort an Array by keys based on another Array?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-07 16:24:01


Re: Sort an Array by keys based on another Array?#3
There you go:function sortArrayByArray(array $array, array $orderArray) { $ordered = array(); foreach($orderArray as $key) { if(array_key_exists($key,$array)) { $ordered[$key] = $array[$key]; unset($array[$key]); } } return $ordered + $array;}

posted date: 2008-12-07 16:32:00


Re: Sort an Array by keys based on another Array?#4
function sortArrayByArray(array $toSort, array $sortByValuesAsKeys){ $commonKeysInOrder = array_intersect_key(array_flip($sortByValuesAsKeys), $toSort); $commonKeysWithValue = array_intersect_key($toSort, $commonKeysInOrder); $sorted = array_merge($commonKeysInOrder, $commonKeysWithValue); return $sorted;}

posted date: 2008-12-07 21:27:00


Re: Sort an Array by keys based on another Array?#5
I didn't know you could cast function arguments like that in PHP!

posted date: 2008-12-07 21:34:00


Re: Sort an Array by keys based on another Array?#6
Arrays and classes only.

posted date: 2008-12-07 23:33:00


Re: Sort an Array by keys based on another Array?#7
So you can join 2 arrays with a + sign? I never knew that, I've been using array_merge()!

posted date: 2009-09-24 15:08:00


Re: Sort an Array by keys based on another Array?#8
Is this better than using usort() or uasort()?

posted date: 2009-09-25 11:57:00


Re: Sort an Array by keys based on another Array?#9
IF you have array in your array, you'll have to adapt the function by Eran a little bit...function sortArrayByArray($array,$orderArray) { $ordered = array(); foreach($orderArray as $key => $value) { if(array_key_exists($key,$array)) { $ordered[$key] = $array[$key]; unset($array[$key]); } } return $ordered + $array;}

posted date: 2009-10-29 13:22:00


Re: Sort an Array by keys based on another Array?#10
foreach($orderArray as $key => $v) did the trick for me

posted date: 2011-09-25 18:06:00


Re: Sort an Array by keys based on another Array?#11
You should insert a break statement once value has been found.

posted date: 2011-09-26 06:50:00


Re: Sort an Array by keys based on another Array?#12
thanks a lot for this one :)

posted date: 2011-11-03 01:39:00


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