-1

I have an array in PHP like this :

Array ( [0] => Array ( [0] => black [2] => brown [1] => red )

[1] => Array
    (
        [2] => car
        [0] => bicycle
        [1] => motorcycle
    )

)

How do I get the array, like this :

Result:

Array ( [0] => Array ( [0] => black [1] => red [2] => brown )

[1] => Array
    (

        [0] => bicycle
        [1] => motorcycle
        [2] => car
    )

)

Thanks

wp78de
  • 18,207
  • 7
  • 43
  • 71

2 Answers2

0

Use the krsort() function to sort an associative array in descending order, according to the key.

Mr. Aniket
  • 83
  • 11
  • ksort only exist in 1 dimension array, how to implement ksort on multidimensional array? thank's – I Nyoman Yoga Dec 09 '17 at 16:24
  • Have a look for answers of this question , may be it will help you [link](https://stackoverflow.com/questions/16306416/sort-php-multi-dimensional-array-based-on-key) – Mr. Aniket Dec 09 '17 at 16:27
0

You could try like this perhaps:

$arr=array(
    array(2=>'red',1=>'green',3=>'black'),
    array(1=>'pink',3=>'blue',2=>'yellow')
);
array_walk( $arr, function(&$v,$k){
    ksort($v);
});
echo '<pre>', print_r( $arr, true ), '</pre>';

Outputs:

Array
(
    [0] => Array
        (
            [1] => green
            [2] => red
            [3] => black
        )

    [1] => Array
        (
            [1] => pink
            [2] => yellow
            [3] => blue
        )

)
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46