I have some code that builds an array with numeric keys. The keys are not in order when the array is built:

array(
  4 => '4444',
  2 => '2222',
  6 => '6666',
  1 => '1111',
  0 => '0000',
);

How can I make this array so it is:

array(
  0 => '0000',
  1 => '1111',
  2 => '2222',
  4 => '4444',
  6 => '6666',
);

Comments

briansea’s picture

calling ksort($YourArray); should do it.
http://www.php.net/manual/en/function.ksort.php

--Brian Seagraves--
http://brianseagraves.com

mattyoung’s picture

Thanks for the info.

I just learn:

array_merge($myArray)

will remake all the keys in numeric sequence. I needed ksrot() but stumble on this.

jaypan’s picture

That's not what array_merge is for. You just found an interesting result of only adding one array to it instead of two.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.