I'm not 100% sure on what your intent is. To simply sort an array based on value but assign new keys, use sort()
:
sort($array);
print_r($array);
Keys aren't preserved with this particular function. Output:
Array
(
[0] => apple
[1] => banana
[2] => pear
)
But if you want to sort the array by key value use ksort()
:
ksort($array);
print_r($array);
Output:
Array
(
[3] => apple
[5] => pear
[23] => banana
)
That will preserve the keys however. To reassign keys for an array from 0 onwards use array_values()
on the result:
ksort($array);
$array_with_new_keys = array_values($array); // sorted by original key order
print_r($array_with_new_keys);
Output:
Array
(
[0] => apple
[1] => pear
[2] => banana
)