Answers for "sort in php"

PHP
2

php sorting functions

<?php
$fruit = array("apple","banana","mango","orange","strawbary");

sort($fruit);       //arrange in ascending order
echo "<pre>";
print_r($fruit);

rsort( $fruit);     //sort in descending order
foreach($fruit as $val)
{
    echo $val."<br>";
}

$girl = array("krisha"=>20,"yashvi"=>30,"ritu"=>4,"pinal"=>80);
asort($girl);       //sort in ascending order according to value
print_r($girl);

ksort($girl);   //sort in ascending order according to key
print_r($girl);     

arsort($girl);      //sort in descending order according to value
print_r($girl);

krsort($girl);      //sort in descending order according to key
print_r($girl);
?>
Posted by: Guest on September-06-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
3

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";
}
?>
//Would output:
c = apple
b = banana
d = lemon
a = orange
Posted by: Guest on April-22-2020
1

php sort hight to low

$letters=array("b","a","c");
arsort($letters); //referse sort an array ("c","a","b")
Posted by: Guest on December-05-2020
0

array sort php

<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo $val;
}
/*
OUTPUT:
apple
banana
lemon
orange
*/
?>
Posted by: Guest on May-14-2020
1

php sort array by value

$price = array();
foreach ($inventory as $key => $row)
{
    $price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
Posted by: Guest on June-19-2021

Browse Popular Code Answers by Language