Answers for "php sort array by value"

PHP
10

php sort array by key

$weight = [
    'Pete' => 75, 
    'Benjamin' => 89,
    'Jonathan' => 101
  ];  	
  ksort($weight);
Posted by: Guest on December-04-2019
1

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
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
1

php store sorted array

$array = array();
$sorted_array = $array;
asort($sorted_array);
Posted by: Guest on March-21-2020
2

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
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

Code answers related to "php sort array by value"

Browse Popular Code Answers by Language