Create numerical-key array from existing string-key array, with a given string-key as first item

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












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' => ['locale' => 'en_US', 'code' => 'en', 'displayName' => 'English'],
'sv' => ['locale' => 'sv_SE', 'code' => 'sv', 'displayName' => 'Swedish'],
'de' => ['locale' => 'de_DE', 'code' => 'de', 'displayName' => 'German']
];

$currentLanguage = 'sv';


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


Result:



 array (
0 =>
array (
'locale' => 'sv_SE',
'code' => 'sv',
'displayName' => 'Swedish',
),
1 =>
array (
'locale' => 'en_US',
'code' => 'en',
'displayName' => 'English',
),
2 =>
array (
'locale' => 'de_DE',
'code' => 'de',
'displayName' => '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 => $value )
if( $key != $currentLanguage )
$langugeList = $value;
;






share|improve this question



















  • I think you have answered your own question. The last example you show, is simple and easy to understand, I would stick with that.
    – bumperbox
    Feb 9 at 19:02

















up vote
0
down vote

favorite












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' => ['locale' => 'en_US', 'code' => 'en', 'displayName' => 'English'],
'sv' => ['locale' => 'sv_SE', 'code' => 'sv', 'displayName' => 'Swedish'],
'de' => ['locale' => 'de_DE', 'code' => 'de', 'displayName' => 'German']
];

$currentLanguage = 'sv';


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


Result:



 array (
0 =>
array (
'locale' => 'sv_SE',
'code' => 'sv',
'displayName' => 'Swedish',
),
1 =>
array (
'locale' => 'en_US',
'code' => 'en',
'displayName' => 'English',
),
2 =>
array (
'locale' => 'de_DE',
'code' => 'de',
'displayName' => '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 => $value )
if( $key != $currentLanguage )
$langugeList = $value;
;






share|improve this question



















  • I think you have answered your own question. The last example you show, is simple and easy to understand, I would stick with that.
    – bumperbox
    Feb 9 at 19:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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' => ['locale' => 'en_US', 'code' => 'en', 'displayName' => 'English'],
'sv' => ['locale' => 'sv_SE', 'code' => 'sv', 'displayName' => 'Swedish'],
'de' => ['locale' => 'de_DE', 'code' => 'de', 'displayName' => 'German']
];

$currentLanguage = 'sv';


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


Result:



 array (
0 =>
array (
'locale' => 'sv_SE',
'code' => 'sv',
'displayName' => 'Swedish',
),
1 =>
array (
'locale' => 'en_US',
'code' => 'en',
'displayName' => 'English',
),
2 =>
array (
'locale' => 'de_DE',
'code' => 'de',
'displayName' => '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 => $value )
if( $key != $currentLanguage )
$langugeList = $value;
;






share|improve this question











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' => ['locale' => 'en_US', 'code' => 'en', 'displayName' => 'English'],
'sv' => ['locale' => 'sv_SE', 'code' => 'sv', 'displayName' => 'Swedish'],
'de' => ['locale' => 'de_DE', 'code' => 'de', 'displayName' => 'German']
];

$currentLanguage = 'sv';


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


Result:



 array (
0 =>
array (
'locale' => 'sv_SE',
'code' => 'sv',
'displayName' => 'Swedish',
),
1 =>
array (
'locale' => 'en_US',
'code' => 'en',
'displayName' => 'English',
),
2 =>
array (
'locale' => 'de_DE',
'code' => 'de',
'displayName' => '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 => $value )
if( $key != $currentLanguage )
$langugeList = $value;
;








share|improve this question










share|improve this question




share|improve this question









asked Feb 9 at 12:40









BadCash

1265




1265











  • I think you have answered your own question. The last example you show, is simple and easy to understand, I would stick with that.
    – bumperbox
    Feb 9 at 19:02

















  • I think you have answered your own question. The last example you show, is simple and easy to understand, I would stick with that.
    – bumperbox
    Feb 9 at 19:02
















I think you have answered your own question. The last example you show, is simple and easy to understand, I would stick with that.
– bumperbox
Feb 9 at 19:02





I think you have answered your own question. The last example you show, is simple and easy to understand, I would stick with that.
– bumperbox
Feb 9 at 19:02











1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










PHP has a function built in for this: array_values()



<?php

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

var_dump(array_values($allLanguages));


Outputs:



array(3) 
[0]=>
array(3)
["locale"]=>
string(5) "en_US"
["code"]=>
string(2) "en"
["displayName"]=>
string(7) "English"

[1]=>
array(3)
["locale"]=>
string(5) "sv_SE"
["code"]=>
string(2) "sv"
["displayName"]=>
string(7) "Swedish"

[2]=>
array(3)
["locale"]=>
string(5) "de_DE"
["code"]=>
string(2) "de"
["displayName"]=>
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:



<?php

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

$currentLanguage = 'sv';

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

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

var_dump($allLanguages);


Live Example




As a one liner:



<?php

$allLanguages = array_values([$currentLanguage => $allLanguages[$currentLanguage]] + $allLanguages);


Live Example




As a helper function:



<?php

function processLanguages($current, $languages)
return array_values([$current => $languages[$current]] + $languages);


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

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


Live Example






share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187182%2fcreate-numerical-key-array-from-existing-string-key-array-with-a-given-string-k%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    PHP has a function built in for this: array_values()



    <?php

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

    var_dump(array_values($allLanguages));


    Outputs:



    array(3) 
    [0]=>
    array(3)
    ["locale"]=>
    string(5) "en_US"
    ["code"]=>
    string(2) "en"
    ["displayName"]=>
    string(7) "English"

    [1]=>
    array(3)
    ["locale"]=>
    string(5) "sv_SE"
    ["code"]=>
    string(2) "sv"
    ["displayName"]=>
    string(7) "Swedish"

    [2]=>
    array(3)
    ["locale"]=>
    string(5) "de_DE"
    ["code"]=>
    string(2) "de"
    ["displayName"]=>
    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:



    <?php

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

    $currentLanguage = 'sv';

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

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

    var_dump($allLanguages);


    Live Example




    As a one liner:



    <?php

    $allLanguages = array_values([$currentLanguage => $allLanguages[$currentLanguage]] + $allLanguages);


    Live Example




    As a helper function:



    <?php

    function processLanguages($current, $languages)
    return array_values([$current => $languages[$current]] + $languages);


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

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


    Live Example






    share|improve this answer



























      up vote
      0
      down vote



      accepted










      PHP has a function built in for this: array_values()



      <?php

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

      var_dump(array_values($allLanguages));


      Outputs:



      array(3) 
      [0]=>
      array(3)
      ["locale"]=>
      string(5) "en_US"
      ["code"]=>
      string(2) "en"
      ["displayName"]=>
      string(7) "English"

      [1]=>
      array(3)
      ["locale"]=>
      string(5) "sv_SE"
      ["code"]=>
      string(2) "sv"
      ["displayName"]=>
      string(7) "Swedish"

      [2]=>
      array(3)
      ["locale"]=>
      string(5) "de_DE"
      ["code"]=>
      string(2) "de"
      ["displayName"]=>
      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:



      <?php

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

      $currentLanguage = 'sv';

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

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

      var_dump($allLanguages);


      Live Example




      As a one liner:



      <?php

      $allLanguages = array_values([$currentLanguage => $allLanguages[$currentLanguage]] + $allLanguages);


      Live Example




      As a helper function:



      <?php

      function processLanguages($current, $languages)
      return array_values([$current => $languages[$current]] + $languages);


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

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


      Live Example






      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        PHP has a function built in for this: array_values()



        <?php

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

        var_dump(array_values($allLanguages));


        Outputs:



        array(3) 
        [0]=>
        array(3)
        ["locale"]=>
        string(5) "en_US"
        ["code"]=>
        string(2) "en"
        ["displayName"]=>
        string(7) "English"

        [1]=>
        array(3)
        ["locale"]=>
        string(5) "sv_SE"
        ["code"]=>
        string(2) "sv"
        ["displayName"]=>
        string(7) "Swedish"

        [2]=>
        array(3)
        ["locale"]=>
        string(5) "de_DE"
        ["code"]=>
        string(2) "de"
        ["displayName"]=>
        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:



        <?php

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

        $currentLanguage = 'sv';

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

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

        var_dump($allLanguages);


        Live Example




        As a one liner:



        <?php

        $allLanguages = array_values([$currentLanguage => $allLanguages[$currentLanguage]] + $allLanguages);


        Live Example




        As a helper function:



        <?php

        function processLanguages($current, $languages)
        return array_values([$current => $languages[$current]] + $languages);


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

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


        Live Example






        share|improve this answer















        PHP has a function built in for this: array_values()



        <?php

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

        var_dump(array_values($allLanguages));


        Outputs:



        array(3) 
        [0]=>
        array(3)
        ["locale"]=>
        string(5) "en_US"
        ["code"]=>
        string(2) "en"
        ["displayName"]=>
        string(7) "English"

        [1]=>
        array(3)
        ["locale"]=>
        string(5) "sv_SE"
        ["code"]=>
        string(2) "sv"
        ["displayName"]=>
        string(7) "Swedish"

        [2]=>
        array(3)
        ["locale"]=>
        string(5) "de_DE"
        ["code"]=>
        string(2) "de"
        ["displayName"]=>
        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:



        <?php

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

        $currentLanguage = 'sv';

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

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

        var_dump($allLanguages);


        Live Example




        As a one liner:



        <?php

        $allLanguages = array_values([$currentLanguage => $allLanguages[$currentLanguage]] + $allLanguages);


        Live Example




        As a helper function:



        <?php

        function processLanguages($current, $languages)
        return array_values([$current => $languages[$current]] + $languages);


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

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


        Live Example







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Feb 17 at 12:22


























        answered Feb 17 at 12:08









        Luke

        1486




        1486






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187182%2fcreate-numerical-key-array-from-existing-string-key-array-with-a-given-string-k%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods