Sort Array in PHP by Value and Maintain Index Association

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



Leave a reply



Submit