Answers for "sort array of associative arrays php"

PHP
3

php sort by associative array value

//php 7+
usort($inventory, function ($item1, $item2) {
    return $item1['price'] <=> $item2['price'];
});
Posted by: Guest on February-02-2021
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);
Posted by: Guest on August-25-2020
0

sort an array in php manually

// take an array with some elements
$array = array('a','z','c','b');
// get the size of array
$count = count($array);
echo "<pre>";
// Print array elements before sorting
print_r($array);
for ($i = 0; $i < $count; $i++) {
    for ($j = $i + 1; $j < $count; $j++) {
        if ($array[$i] > $array[$j]) {
            $temp = $array[$i];
            $array[$i] = $array[$j];
            $array[$j] = $temp;
        }
    }
}
echo "Sorted Array:" . "<br/>";
print_r($array);
Posted by: Guest on July-09-2021

Code answers related to "sort array of associative arrays php"

Browse Popular Code Answers by Language