I am not 100% sure about your intentions. To simply sort the array based on the value, but assign new keys, use sort() :
sort($array); print_r($array);
Keys are not stored with this particular function. Exit:
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 )
This will save the keys. To reassign keys to an array with 0, use array_values() as a 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 )
cletus
source share