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

Clash 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;
;
php array sorting collections
add a comment |Â
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;
;
php array sorting collections
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
add a comment |Â
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;
;
php array sorting collections
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;
;
php array sorting collections
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Feb 17 at 12:22
answered Feb 17 at 12:08
Luke
1486
1486
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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