Using the array_keys function array_keys you can determine the key index:
$keys = array_flip(array_keys($input)); printf("Index of '%s' is: %d\n", $key, $keys[$key]);
To insert an array at a specific position (for example, at the beginning), there is an array_splice function. This way you can create an array to insert, remove the value from the old place and paste it into:
$key = 'two'; $value = $input[$key]; unset($input[$key]); array_splice($input, 0, 0, array($key => $value));
Something like this is possible using the array join operator , but only because you want to go to the beginning:
$key = 'two'; $value = $input[$key]; unset($input[$key]); $result = array($key => $value) + $input;
But I think this may have more overhead than array_splice .
hakre source share