sort multi array php
$keys = array_column($array, 'Price');
array_multisort($keys, SORT_ASC, $array);
print_r($array);
sort multi array php
$keys = array_column($array, 'Price');
array_multisort($keys, SORT_ASC, $array);
print_r($array);
sort multidimensional array in php
<?php
function sortByPrice($a, $b){
return $a['price'] > $b['price'];
}
$items = [
['label' => 'cake', 'name' => 'Cake', 'price' => 150],
['label' => 'pizza', 'name' => 'Pizza', 'price' => 250],
['label' => 'puff', 'name' => 'Veg Puff', 'price' => 20],
['label' => 'samosa', 'name' => 'Samosa', 'price' => 14]
];
//Sort by Price
usort($items, 'sortByPrice');
//print_r($items);
print "<br/> After Sort by Price printing: <br/>";
foreach($items as $item){
print $item['name']." ".$item['price']."<br/>";
}
$newArray = array_column($items, 'price', 'name');
// find max, min, and toal sum of array
$totalExp = array_sum(array_column($items, 'price', 'name'));
$maxPrice = max(array_column($items, 'price', 'name'));
$minPrice = min(array_column($items, 'price', 'name'));
print "Total Expenses : ".$totalExp."<br/>";
print "What is Costly Item : ".$maxPrice.' ('.array_search($maxPrice, $newArray).")<br/>";
print "What is Cheap Item : ".$minPrice.' ('.array_search($minPrice, $newArray).")<br/>";
?>
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us