"sort array in php by value and maintain index association" Code Answer

1

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.

By carl_stephens-8fbe5bfb68dc on November 30 2022

Answers related to “sort array in php by value and maintain index association”

Only authorized users can answer the search term. Please sign in first, or register a free account.