Search
 
SCRIPT & CODE EXAMPLE
 

PHP

associative array sorting by value in php

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

php sort associative array by specific value

$price = array_column($inventory, 'price');

array_multisort($price, SORT_DESC, $inventory);
Comment

php sort by associative array value

//php 7+
usort($inventory, function ($item1, $item2) {
    return $item1['price'] <=> $item2['price'];
});
Comment

sort array php

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

php sort array by value

$price = array();
foreach ($inventory as $key => $row)
{
    $price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
Comment

sort an array in php manually

// 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);
Comment

sort array php

$carsArray = array( "Volvo", "Honda", "Toyota", "BMW");

sort($carsArray);

$no_car = count($carsArray);

for( $x=0; $x<$no_car; $x++ )
{
  echo $carsArray[$x];
  echo "<br>";
}
Comment

php sort array

$array_temp_id = array_column($companions, 'temp_id');
array_multisort($array_temp_id, SORT_DESC, $companions);
Comment

PREVIOUS NEXT
Code Example
Php :: Clear php cache 
Php :: laravel 8: bootstrap 
Php :: for each php 
Php :: check for an existing user laravel eloquent 
Php :: search function using php for database entries 
Php :: hasone relation in laravel 
Php :: php try catch 
Php :: php datetime set timezone 
Php :: yii2 html a 
Php :: magento getcollection get first 
Php :: laravel 6 get user id 
Php :: comparing floats php 
Php :: redirect compact laravel 
Php :: php copy image from remote to local server 
Php :: laravel fixed character limit 
Php :: laravel add crf token form 
Php :: laravel validation unique two columns 
Php :: json_encode() in php 
Php :: php textarea replace newline with br 
Php :: how to get all post fields in wordpress 
Php :: hide env file in laravel 
Php :: pagination with search query in laravel 
Php :: How to Show the Logged in Username in the WordPress 
Php :: laravel form put method 
Php :: Notice: Undefined variable: _SESSION in C:xampphtdocspracticeheader.php on line 7 
Php :: php conditionally remove element from array 
Php :: varchar max length define laravel migration 
Php :: You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file laravel 
Php :: php get all in object as array 
Php :: how increase php upload size in wordpress 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =