I want to sort the key of the following array. I use ksort(), but i don't know how to use it. Any idea?
<?php
$a = array(
'kuy' => 'kuy',
'apple' => 'apple',
'thida' => 'thida',
'vanna' => 'vanna',
'ravy' => 'ravy'
);
$b = ksort($a);
echo "<pre>";
print_r($b);
echo "</pre>";
?>
Accepted Answer
ksort()
sorts the array itself and does not create a sorted copy
$a = array(
'kuy' => 'kuy',
'apple' => 'apple',
'thida' => 'thida',
'vanna' => 'vanna',
'ravy' => 'ravy'
);
ksort($a);
echo "<pre>";
print_r($a);
echo "</pre>";
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki
Comments
Maybe
ksort
is not the right function. It depends on what you want to sort. Here is a list of all sort functions. To learn how to use a function, have a look at its documentation, e.g. php.net/manual/en/function.ksort.phpso what does print_r($b) show you? Have you read this php.net/manual/en/function.ksort.php? It is hard to be any clearer really.
possible duplicate of how to sort an associative array in php