//php
$a=array(array("id" => 4,"name"=> "Dog"),array("id" => 1,"name"=> "Cat"),array("id" => 3,"name"=> "Hourse"),array("id" => 2,"name"=> "Bear"),array("id" => 5,"name"=> "Zebra"));
$b = array_column($a,'name'); // which column needed to be sorted
array_multisort($b,SORT_ASC,$a); // sorts the array $a with respective of aray $b
$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
//php 7+
usort($inventory, function ($item1, $item2) {
return $item1['price'] <=> $item2['price'];
});
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val
";
}
?>
//Would output:
c = apple
b = banana
d = lemon
a = orange
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
// 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);
$carsArray = array( "Volvo", "Honda", "Toyota", "BMW");
sort($carsArray);
$no_car = count($carsArray);
for( $x=0; $x<$no_car; $x++ )
{
echo $carsArray[$x];
echo "<br>";
}
$array_temp_id = array_column($companions, 'temp_id');
array_multisort($array_temp_id, SORT_DESC, $companions);