Sort array in PHP by value and maintain index association
This should work using asort():
<?php
$array = array(
'john' => 2,
'adam' => 3,
'ben' => 10,
'tim' => 1,
);
asort($array, SORT_NUMERIC);
print_r($array);
?>
output:
Array
(
[tim] => 1
[john] => 2
[adam] => 3
[ben] => 10
)
Checkout the demo.
Sort associative array according to value without deleting keys
You can use arsort()
. From the manual:
arsort — Sort an array in reverse order and maintain index association
Example:
$a = array(
'533ae5a78ead0e8e118b4567' => 1,
'534d5a4b8ead0e5b73294d72' => 45,
'533ee8bc8ead0ec5138b4567' => 32
);
arsort($a);
var_dump($a);
Output:
array(3) {
'534d5a4b8ead0e5b73294d72' =>
int(45)
'533ee8bc8ead0ec5138b4567' =>
int(32)
'533ae5a78ead0e8e118b4567' =>
int(1)
}
Sort array and keep values of keys
Use the uasort function, that should keep the key => value associations intact.
(side note: you can do return $a['points'] - $b['points']
instead of the ifs)
Sort array without replacing index elements in php
A quick Google search normally does the trick...
arsort($tabsent);
From the manual:
arsort — Sort an array in reverse order and maintain index association
How to sort an array of associative arrays by value of a given key in PHP?
You are right, the function you're looking for is array_multisort()
.
Here's an example taken straight from the manual and adapted to your case:
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
As of PHP 5.5.0 you can use array_column()
instead of that foreach:
$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
PHP asort for sorting array items by values ASC?
Since the version 4.1.0 PHP sort is not stable due to sorting algorithm, which cannot take into account both value and key. You have to use your own implementation of comparison considering key in case of equal values. For example you can modify original array values into (key, value) pairs, sort array and transform it into single dimensional back.
$items = ["1" => 10, "11" => 10, "22" => 10];
$callback = function ($a, $b) use ($callback) {
$result = $a['value'] - $b['value'];
if ($result == 0) {
$result = $a['key'] - $b['key'];
}
return $result;
};
array_walk($items, function (&$value, $key) {
$value = ["key" => $key, "value" => $value];
});
usort($items, $callback);
$items = array_combine(array_column($items, 'key'), array_column($items, 'value'));
print_r($items);
Sort array by values while preserving key order
You can extract the keys of your array using array_keys
, sort the values using sort
and then combine the keys back with the sorted values using array_combine
:
$array = array(
'5' => 2,
'6' => 3,
'7' => 10,
'8' => 1
);
$keys = array_keys($array);
sort($array);
$array = array_combine($keys, $array);
print_r($array);
Output:
Array
(
[5] => 1
[6] => 2
[7] => 3
[8] => 10
)
Demo on 3v4l.org
Sorting multidimensional array without losing key values
I think what you're looking for is asort()
instead of usort()
. That'll maintain the key indexes and just sort by values in ascending order.
Related Topics
How to Set 777 Permission on a Particular Folder
Mysqli_Stmt::Execute() Expects Exactly 0 Parameters, 1 Given
Running a Zend Framework Action from Command Line
When Do I Use Static Variables/Functions in PHP
How to Use Break or Continue Within for Loop in Twig Template
Php:: How Long to Tmp Files Stay
Strcmp Equivelant for Integers (Intcmp) in PHP
PHP Header Location Redirect Not Working
Unexpected T_Encapsed_And_Whitespace, Expecting T_String or T_Variable or T_Num_String Error
PHP Object Attribute with Dot in Name
Codeigniter Sessions Not Working After Migration
How to Return a Proper Success/Error Message for Jquery .Ajax() Using PHP
Laravel Migration Table Already Exists, But I Want to Add New Not the Older
Calculate Total Seconds in PHP Dateinterval
How to Redirect to Another Page Using PHP
Can't Find Fulltext Index Matching the Column List (Indexes Is Set)
Malformed Utf-8 Characters, Possibly Incorrectly Encoded' in Laravel