-2

How can I sort this array by sub array key (10, 20, 21, 60, 70):

$array = array(
    'Value 1'=>  array (
        '10'=> "-11.34"
    ),

    "Value 2"=>  array (
        '60'=> '-10'
    ),

    "Value 3"=>  array (
        '70'=> '-23.96'
    ),

    "Value 4"=>  array (
        '20'=> '-23.96'
    ),

    "Value 5"=>  array (
        '21'=> '-6.1'
    )       
);

To get at the final this sorting:

Value 1 : 11.34

Value 4 : 20.4

Value 5 : 6.1

Value 2 : 10

Value 3 : 23.96

Using this function:

function sortByValueKey($a, $b) {
    $keysA = array_keys($a);
    $keysB = array_keys($b);
    return $keysA[0] > $keysB[0];
}

I don't find my text values anymore. Any Ideas to solve this Issue

Bizboss
  • 7,792
  • 27
  • 109
  • 174
  • Possible duplicate of [Sorting a multidimentional array using PHP](http://stackoverflow.com/questions/25090227/sorting-a-multidimentional-array-using-php) – Neobugu Feb 25 '16 at 15:33
  • You should write more descriptive titles, so people can guess what do you need in a quick view – Asur Feb 25 '16 at 15:42

1 Answers1

1

Just use this code. $array is the content of your described array

<?php
function sortByValueKey($a, $b) {
    $keysA = array_keys($a);
    $keysB = array_keys($b);
    return $keysA[0] > $keysB[0];
}

uasort($array, 'sortByValueKey');
Kordi
  • 2,405
  • 1
  • 14
  • 13
  • Thanks for your answer, I have tried it, but when I make a var_dump on my array I don't find my text values anymore (Value1, Value2...), any Ideas to fix that issue ? – Bizboss Feb 25 '16 at 16:06
  • fixed it! Just use uasort instead of usort! – Kordi Feb 25 '16 at 16:20