Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Sort Multi-dimensional Array by Value

Sort Multi-dimensional Array by Value
Above or PHP 5.3
‘a1′,’records’ => ’10’),
array( ‘name’ => ‘b3’, ‘records’ => ’31’),
array( ‘name’ => ‘c4’, ‘records’ => ‘1’),
array( ‘name’ => ‘d2’, ‘records’ => ‘5’ )
);

print_r($array);
// For DESC
usort($array, function($a, $b) { return $b[‘name’] – $a[‘name’]; });
// For ASC
//usort($array, function ($a, $b) { return $a[‘records’] – $b[‘records’]; });

echo ‘—————- Print Sorted array —————-‘;
print_r($array);

//If you are still on PHP 5.2 or earlier, you’ll have to define a sorting function first:

function sortByOrder($a, $b) {
// FOR DESC
return $b[‘records’] – $a[‘records’];
// FOR ASC
//return $a[‘records’] – $b[‘records’];
}

usort($array, ‘sortByOrder’);
print_r($array);

The post Sort Multi-dimensional Array by Value appeared first on PHPCORELAB.



This post first appeared on Quick Reference For PHP, please read the originial post: here

Share the post

Sort Multi-dimensional Array by Value

×

Subscribe to Quick Reference For Php

Get updates delivered right to your inbox!

Thank you for your subscription

×