Sorting associate arrays in php with asort

The asort() function sorts an associative array in ascending order, according to the value.

Example

<?php
$paper [1] = 36;
$paper [21] = 12;
$paper [19] = 2;
$paper [3] = 5;
$paper [8] = 25;
$paper [23] = 16;
$paper [13] = 35;

var_dump ( $paper );

asort ( $paper );

echo "Sorted array <br>";

var_dump ( $paper );

?>

Output

array (size=7)
  1 => int 36
  21 => int 12
  19 => int 2
  3 => int 5
  8 => int 25
  23 => int 16
  13 => int 35

Sorted array

array (size=7)
  19 => int 2
  3 => int 5
  21 => int 12
  23 => int 16
  8 => int 25
  13 => int 35
  1 => int 36
Share

You may also like...