Reputation: 4553
I have an array look like below.
$array[0]['keyword'] = 'cricket ';
$array[0]['noofhits'] = '26';
$array[1]['keyword'] = 'food ';
$array[1]['noofhits'] = '17';
$array[2]['keyword'] = 'mypax';
$array[2]['noofhits'] = '22';
$array[3]['keyword'] = 'next';
$array[3]['noofhits'] = '22';
$array[4]['keyword'] = 'nextbutton';
$array[4]['noofhits'] = '22';
$array[5]['keyword'] = 'picture';
$array[5]['noofhits'] = '18';
I want to sort the array using the noofhits. How can I do? Advance Thanks for your advice.
Soory for the previous one.Thanks for your answers.
Upvotes: 0
Views: 1640
Reputation: 655785
Use usort
with a custom comparison function:
function cmp($a, $b) {
return $a['noofhits'] - $b['noofhits'];
}
usort($array, 'cmp');
usort
expects the comparison function to return three different value:
So we can simply subtract the value of b from a. If a’s value is greater than b’s value, the subtraction yields a positive integer; if a’s value is equal to b’s value, it yields 0; and if a’s value is less than b’s value, it yields a negative value.
Upvotes: 9