-1

Here is my array:

    Array ( [nse_h_a] => 2351 [nse_h_b] => 6119 [nse_h_c] => 2496 [nse_h_d] => 402 [nse_h_e] => 180 )

And I want to sort the array by key values not

sort, ksort and krsort dont work.

Is it possible?

  • Why would you think that ksort/krsort would work? http://php.net/ksort `Sort an array by key` – Marc B Sep 28 '16 at 16:31
  • `asort()` if you want to maintain the keys, otherwise `sort()` should work fine – iainn Sep 28 '16 at 16:31
  • Can you explain why `sort` did not work? It certainly should have worked. – Don't Panic Sep 28 '16 at 16:38
  • I solve it! function aksort(&$array,$valrev=false,$keyrev=false) { if ($valrev) { arsort($array); } else { asort($array); } $vals = array_count_values($array); $i = 0; foreach ($vals AS $val=>$num) { $first = array_splice($array,0,$i); $tmp = array_splice($array,0,$num); if ($keyrev) { krsort($tmp); } else { ksort($tmp); } $array = array_merge($first,$tmp,$array); unset($tmp); $i = $num; } } – N Javier Romero Sep 28 '16 at 17:03

1 Answers1

0

Use asort().

$isShorted = asort($myArray);
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49