PHP – Sort Array by Key – ksort() and krsort()

You can sort arrays by key with ksort() and krsort().

<?php

$inventory = array(
	'orange' => 3,
	'blue' => 5,
	'green' => 7,
	'red' => 12,
	'white' => 8,
	'teal' => 1
);

print_r($inventory);

print "<br>";

ksort($inventory);

print_r($inventory);

print "<br>";

krsort($inventory);

print_r($inventory);

?>

Be the first to comment

Leave a Reply