Create numerical-key array from existing string-key array, with a given string-key as first item I have an array of available languages for my website, with a short ISO country code as the key for each language. I need to create a new array that contains the same info, but with numerical keys and where the first item in the new array is the current language.

I have this, which works fine:

$allLanguages = [
    'en' =&gt; ['locale' =&gt; 'en_US', 'code' =&gt; 'en', 'displayName' =&gt; 'English'],
    'sv' =&gt; ['locale' =&gt; 'sv_SE', 'code' =&gt; 'sv', 'displayName' =&gt; 'Swedish'],
    'de' =&gt; ['locale' =&gt; 'de_DE', 'code' =&gt; 'de', 'displayName' =&gt; 'German']
];

$currentLanguage = 'sv';


// Create new numerical-ordered array with current language info as first element
$langugeList = [];
foreach( $allLanguages as $key =&gt; $value ){
    if( $key == $currentLanguage ) 
        array_unshift($langugeList, $value);
    else 
        array_push($langugeList, $value);
};


Result:

    array (
        0 =&gt;
            array (
                'locale' =&gt; 'sv_SE',
                'code' =&gt; 'sv',
                'displayName' =&gt; 'Swedish',
            ),
        1 =&gt;
            array (
                'locale' =&gt; 'en_US',
                'code' =&gt; 'en',
                'displayName' =&gt; 'English',
            ),
        2 =&gt;
            array (
                'locale' =&gt; 'de_DE',
                'code' =&gt; 'de',
                'displayName' =&gt; 'German',
            ),
    )


Is there another, more elegant way of doing this? 

I first had the idea to copy the array values and then sort them using usort() (keeping the original order except for the current language), but that doesn't seem like an improvement to neither performance nor "elegancy".

I just realized that one option would be

$langugeList = [ $allLanguages[$currentLanguage] ];
foreach( $allLanguages as $key =&gt; $value ){
    if( $key != $currentLanguage )
        $langugeList[] = $value;
};
PHP has a function built in for this: array_values()

&lt;?php

$allLanguages = [
    'en' =&gt; ['locale' =&gt; 'en_US', 'code' =&gt; 'en', 'displayName' =&gt; 'English'],
    'sv' =&gt; ['locale' =&gt; 'sv_SE', 'code' =&gt; 'sv', 'displayName' =&gt; 'Swedish'],
    'de' =&gt; ['locale' =&gt; 'de_DE', 'code' =&gt; 'de', 'displayName' =&gt; 'German']
];

var_dump(array_values($allLanguages));


Outputs:

array(3) {
  [0]=&gt;
  array(3) {
    ["locale"]=&gt;
    string(5) "en_US"
    ["code"]=&gt;
    string(2) "en"
    ["displayName"]=&gt;
    string(7) "English"
  }
  [1]=&gt;
  array(3) {
    ["locale"]=&gt;
    string(5) "sv_SE"
    ["code"]=&gt;
    string(2) "sv"
    ["displayName"]=&gt;
    string(7) "Swedish"
  }
  [2]=&gt;
  array(3) {
    ["locale"]=&gt;
    string(5) "de_DE"
    ["code"]=&gt;
    string(2) "de"
    ["displayName"]=&gt;
    string(6) "German"
  }
}


Live Example



If you combine that function with this answer, you can move the desired array element to the top of the array before removing the keys:

&lt;?php

$allLanguages = [
    'en' =&gt; ['locale' =&gt; 'en_US', 'code' =&gt; 'en', 'displayName' =&gt; 'English'],
    'sv' =&gt; ['locale' =&gt; 'sv_SE', 'code' =&gt; 'sv', 'displayName' =&gt; 'Swedish'],
    'de' =&gt; ['locale' =&gt; 'de_DE', 'code' =&gt; 'de', 'displayName' =&gt; 'German']
];

$currentLanguage = 'sv';

// Move the desired language to the front of the array
$allLanguages = [$currentLanguage =&gt; $allLanguages[$currentLanguage]] + $allLanguages;

// Get a new array with just the values
$allLanguages = array_values($allLanguages);

var_dump($allLanguages);


Live Example



As a one liner:

&lt;?php

$allLanguages = array_values([$currentLanguage =&gt; $allLanguages[$currentLanguage]] + $allLanguages);


Live Example



As a helper function:

&lt;?php

function processLanguages($current, $languages) {
    return array_values([$current =&gt; $languages[$current]] + $languages);
}

$allLanguages = [
    'en' =&gt; ['locale' =&gt; 'en_US', 'code' =&gt; 'en', 'displayName' =&gt; 'English'],
    'sv' =&gt; ['locale' =&gt; 'sv_SE', 'code' =&gt; 'sv', 'displayName' =&gt; 'Swedish'],
    'de' =&gt; ['locale' =&gt; 'de_DE', 'code' =&gt; 'de', 'displayName' =&gt; 'German']
];

var_dump(processLanguages('sv', $allLanguages));


Live Example