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

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

PREVIOUS NEXT
Code Example
Php :: php string contains 
Php :: remove notices php 
Php :: how to bulk insert array into sql php 
Php :: array reduce associative array php 
Php :: phpcs 
Php :: php run command line 
Php :: How to request and display data from db in larave 
Php :: how send user to 404 page if not exist page in laravel 
Php :: Flutter Error - Migrate to android studio - MAC OS 
Php :: php requuire once 
Php :: php array to string 
Php :: how to send ajax request in laravel 
Php :: php eliminar elementos vacios array 
Php :: laravel db query 
Php :: $this- attribute laravel 
Php :: replace php 
Php :: php text to html 
Php :: Composer Fatal error: Call to undefined function SymfonyPolyfillMbstringiconv() in phar 
Php :: php string parse with separator explode 
Php :: add to json object php 
Php :: delete bunch of rows in laravel 
Php :: laravel file permissions 
Php :: php date + 30 days 
Php :: laravel delete 
Php :: Modes of file reading php 
Php :: laravel auth user in constructor 
Php :: laravel eloquent get first 
Php :: <a href="<?php echo base_url(); ?"somelink</a 
Php :: how to execute cmd command in php 
Php :: delete file in php 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =