php sort array of objects by key code example
Example 1: php sort array by object value
$questions = [
{ id: 1, ordinal: 55 },
{ id: 2, ordinal: 67 },
{ id: 3, ordinal: 32 },
];
function sortByOrdinal($param1, $param2) {
return strcmp($param1->ordinal, $param2->ordinal);
}
usort($questions, "sortByOrdinal");
Example 2: 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);