0

Is it possible to sort an array without changing the STARTING key? If so, how?

I have an array that starts with key 1 (I need it to start with key 1) but as expected, when I sort the array, the starting key becomes 0.

Example code below:

<?php

$array = array(
    1 => 'string1',
    2 => 'string2',
    3 => 'string3',
);

print_r($array); //Outputs 'Array ( [1] => string1 [2] => string2 [3] => string3 )'

sort($array); 

print_r($array); //Outputs 'Array ( [0] => string1 [1] => string2 [2] => string3 )'
?>

So with this example, I need $array to start with key [1] after sort().

asort() is not an option, as I need the array to be in numerical order (again, starting from 1).

Could anyone help?

JayIsTooCommon
  • 1,714
  • 2
  • 20
  • 30

1 Answers1

0

use uasort. refer following link.

http://php.net/manual/en/function.uasort.php

Hytool
  • 1,358
  • 1
  • 7
  • 22