1

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>";
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Tepken Vannkorn
  • 9,648
  • 14
  • 61
  • 86
  • 6
    Maybe `ksort` is not the right function. It depends on what you want to sort. [Here is a list of all sort functions](http://php.net/manual/en/array.sorting.php). To learn how to use a function, **have a look at its documentation**, e.g. http://php.net/manual/en/function.ksort.php – Felix Kling Jun 27 '11 at 11:43
  • so what does print_r($b) show you? Have you read this http://www.php.net/manual/en/function.ksort.php? It is hard to be any clearer really. – vascowhite Jun 27 '11 at 11:45
  • possible duplicate of [how to sort an associative array in php](http://stackoverflow.com/questions/5497482/how-to-sort-an-associative-array-in-php) – Bobby Jun 27 '11 at 15:17

9 Answers9

4

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>";
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
2

ksort does not return an array. It just sorts the original array, and returns bool "Returns TRUE on success or FALSE on failure. "

So your sorted array is $a, not $b. see it here : http://codepad.org/zMTFTPGf

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
2

You find your answer there: http://php.net/manual/de/function.ksort.php

Use it just like:

ksort($a);

then $a is sorted.

Alex
  • 365
  • 3
  • 11
2

If you don't want to preserve the original order of $a then use :-

ksort($a);
print_r($a);

If you want to keep $a, but also want a sorted version use:-

$b = $a;
ksort($b);
print_r($b);

As said in my comment the manual page makes it quite clear. http://www.php.net/manual/en/function.ksort.php

vascowhite
  • 18,120
  • 9
  • 61
  • 77
1

ksort returns boolean value and sort the original array so you should print $a instead of $b because $b is a boolean value returned by the ksort which is either true or false depending on the result of ksort

ksort($a);    
print_r($a);
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

ksort returns a boolean - whether the sort succeeded or not. It sorts the array in-place - where it changes the array variable rather than returns a sorted copy.

Try:

ksort($a);
print_r($a);
Ross
  • 46,186
  • 39
  • 120
  • 173
1

ksort takes its argument by reference and modifies it directly, the return value just indicates syccess or failure.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
0

ksort returns a boolean on whether it was successful or not, it doesn't return another sorted array. It changes the original array.

print_r($a);
mikeq
  • 817
  • 5
  • 5
0

As Felix said look at the documentation. you can also look at the example here

abhiasawa
  • 1,330
  • 1
  • 10
  • 20