php sort array of arrays code example
Example 1: php sort multidimensional array
array_multisort(array_map(function($element) {
return $element['order'];
}, $array), SORT_ASC, $array);
print_r($array);
Example 2: sort array of array php
sort()
rsort()
asort()
ksort()
arsort()
krsort()
$array = array(1, 3, 2);
sort($array)
Example 3: sort multi array php
$keys = array_column($array, 'Price');
array_multisort($keys, SORT_ASC, $array);
print_r($array);
Example 4: php sort by associative array value
usort($inventory, function ($item1, $item2) {
return $item1['price'] <=> $item2['price'];
});
Example 5: php sort array of array by key
$inventory = [
['price' => 10.99, 'product' => 'foo 1'],
['price' => 5.99, 'product' => 'foo 2'],
['price' => 100, 'product' => 'foo 3'],
];
$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
Example 6: sort array php
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
c = apple
b = banana
d = lemon
a = orange