I have an array where the keys are numeric, non-consecutive, and potentially out of order, like so:

array(
  3 => 'bravo-foxtrot',
  180 => 'romeo-tango-foxtrot-mike',
  24 => 'sierra-oscar-lima',
  1 => 'wilco-tango-foxtrot',
)

What I need to do, is sort them by key (not by value):

array(
  1 => 'wilco-tango-foxtrot',
  3 => 'bravo-foxtrot',
  24 => 'sierra-oscar-lima',
  180 => 'romeo-tango-foxtrot-mike',
)

And then renumber the keys so they're consecutive:

array(
  0 => 'wilco-tango-foxtrot',
  1 => 'bravo-foxtrot',
  2 => 'sierra-oscar-lima',
  3 => 'romeo-tango-foxtrot-mike',
)

Before I try to roll a function of my own to do this, however, I was wondering if there's a builtin php function (or a combination thereof) that would effectively and easily accomplish this? The reason I ask is because I will be doing this repeatedly and heavily, so performance is a potential issue.

I searched the board and googled for renumbering arrays by keys and couldnt find anything fitting this particular situation...Thanks for taking the time to read this 🙂

    With [man]ksort/man and [man]array_values[/man], that is quite easy, actually:

    ksort($array);
    $array = array_values($array);
      Write a Reply...