I feel like I'm repeating myself, sorry :D
Do not hesitate to look at libraries from frameworks.
[CakePHP] made an awesome class which is able to navigate into arrays using a string in dot syntax notation. This library is known as Hash, just look at it.
The method Sort is used to sort an array with keys and values depending of what you put inside the "path" attribute.
It's more useful than others functions, because it sorts the array by keeping the indexes and all the other subvalues contained in this array, including more dimensional sub-arrays.
For your example :
$array = array(
'id1' => array (
'unique' => 3,
),
'id2' => array (
'unique' => 5,
),
'id3' => Array (
'unique' => 1,
),
);
Just write this :
/**
* First param: the array
* Second param : the "path" of the datas to save.
* "{s}" means "Matching any kind of index", for you to sort ALL indexes matching
* the dot "." is used to navigate into the array like an object
* And "unique" is the key which you want to be sorted by its value
* The third param is simply the order method, "ascending" or "descending"
*/
$sorted_array = Hash::sort($array, '{s}.unique', 'asc');
And $sorted_array
will be equal to this :
array (
'id3' => array (
'unique' => 1,
),
'id1' => array (
'unique' => 3,
),
'id2' => array (
'unique' => 5,
),
);