How can i re-order an array from 0 to 10 with a value within the array? [duplicate]
Possible Duplicate:
Sorting a multidimensional array?
I have an a multi dimesional associative array:
[book]=>{
[names]=>{
[name]=>'thing1',
[age]=>'23',
开发者_如何转开发 [nOrder]=>'1'
},
{
[name]=>'thing2',
[age]=>'24',
[nOrder]=>'3'
},
{
[name]=>'thing3',
[age]=>'25',
[nOrder]=>'2'
}
}
Hope that example looks ok, basically I need to re-order the positions of the rows within the "names" value to be in order form 1 to 3 by whatever is in the "nOrder" value.
function cmp( $a, $b ) {
return $a['nOrder'] > $b['nOrder'] ? 1 : -1;
}
usort( $book['names'], 'cmp' );
from usort manual page:
This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.
We pass cmp
as the comparison function to usort()
and it will sort elements of $book['names']
array using that comparison function.
精彩评论