php sort array by key
$weight = [
'Pete' => 75,
'Benjamin' => 89,
'Jonathan' => 101
];
ksort($weight);
php sort array by key
$weight = [
'Pete' => 75,
'Benjamin' => 89,
'Jonathan' => 101
];
ksort($weight);
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);
?>
php sort by associative array value
//php 7+
usort($inventory, function ($item1, $item2) {
return $item1['price'] <=> $item2['price'];
});
php store sorted array
$array = array();
$sorted_array = $array;
asort($sorted_array);
php sort array by value
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
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
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