I'm trying to break up this string,
我想把这条线弄断,
AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh
Into an array like this
变成这样的数组
$lang_codes['chinese'] = "zh";
So the name of the language is the key, and the value is the language code. This is really simple, but I just can't get my head around it. I've taken a brake from programming, too long, obviously...
所以语言的名称是键,值是语言代码。这真的很简单,但我就是想不起来。我已经从编程中退出,太长了,很明显……
I've tried exploding at \n
then using a foreach
, exploding again at =
but I can't seem to piece it together the way I want.
我试过在\n上爆炸,然后用foreach,在=上再次爆炸,但我似乎无法按照我想要的方式将它组合在一起。
4 个解决方案
#1
4
I've tried exploding at \n then using a foreach, exploding again at " = "
我试过在\n上爆炸然后用foreach,在=上爆炸
I think this is exactly the right approach.
我认为这是正确的做法。
$lines = explode("\n", $langs);
$lang_codes = array();
foreach ($lines as $line) {
list ($lang, $code) = explode(" = ", $line, 2);
$lang_codes[$lang] = $code;
}
If you want the language to be in lowercase, as in your example ($lang_codes['chinese']
), you'll need to call strtolower
:
如果您希望语言是小写的,如您的示例($lang_codes['chinese']),您需要调用strtolower:
$lang_codes[strtolower($lang)] = $code;
See the PHP manual for more on these functions:
有关这些函数的更多信息,请参阅PHP手册:
#2
2
Although the explode answers are technically correct, I immediately thought that you were trying to parse an INI file. Here's the simpler approach that does exactly what you want.
虽然这个爆炸性的答案在技术上是正确的,但我立即认为您正在尝试解析INI文件。这里有一种更简单的方法,可以完全满足您的需要。
<?php
$string = "AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh";
$array = array_change_key_case( parse_ini_string( $string ) );
echo $array['chinese'];
#3
0
Mayb you shold do:
Mayb你应当做的事:
$array = explode("\n" , $string);
$finalArray =array();
$foreach ($array as $value){
$array2 = explode( ' = ', $value);
$finalArray[$array2[1]] = $array2[0];
}
#4
0
<?php
$str ="AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh";
$arr = explode("\n", $str);
$lang_mapped = array();
foreach ($arr as $line) {
list ($lang, $code) = explode(" = ", $line, 2);
$lang_mapped[$lang] = $code;
}
$arr = explode("\n", $str);
echo "<pre>";
print_r($lang_mapped);
echo "</pre>";
?>
#1
4
I've tried exploding at \n then using a foreach, exploding again at " = "
我试过在\n上爆炸然后用foreach,在=上爆炸
I think this is exactly the right approach.
我认为这是正确的做法。
$lines = explode("\n", $langs);
$lang_codes = array();
foreach ($lines as $line) {
list ($lang, $code) = explode(" = ", $line, 2);
$lang_codes[$lang] = $code;
}
If you want the language to be in lowercase, as in your example ($lang_codes['chinese']
), you'll need to call strtolower
:
如果您希望语言是小写的,如您的示例($lang_codes['chinese']),您需要调用strtolower:
$lang_codes[strtolower($lang)] = $code;
See the PHP manual for more on these functions:
有关这些函数的更多信息,请参阅PHP手册:
#2
2
Although the explode answers are technically correct, I immediately thought that you were trying to parse an INI file. Here's the simpler approach that does exactly what you want.
虽然这个爆炸性的答案在技术上是正确的,但我立即认为您正在尝试解析INI文件。这里有一种更简单的方法,可以完全满足您的需要。
<?php
$string = "AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh";
$array = array_change_key_case( parse_ini_string( $string ) );
echo $array['chinese'];
#3
0
Mayb you shold do:
Mayb你应当做的事:
$array = explode("\n" , $string);
$finalArray =array();
$foreach ($array as $value){
$array2 = explode( ' = ', $value);
$finalArray[$array2[1]] = $array2[0];
}
#4
0
<?php
$str ="AFRIKAANS = af
ALBANIAN = sq
AMHARIC = am
ARABIC = ar
ARMENIAN = hy
AZERBAIJANI = az
BASQUE = eu
BELARUSIAN = be
BENGALI = bn
BIHARI = bh
BULGARIAN = bg
BURMESE = my
CATALAN = ca
CHEROKEE = chr
CHINESE = zh";
$arr = explode("\n", $str);
$lang_mapped = array();
foreach ($arr as $line) {
list ($lang, $code) = explode(" = ", $line, 2);
$lang_mapped[$lang] = $code;
}
$arr = explode("\n", $str);
echo "<pre>";
print_r($lang_mapped);
echo "</pre>";
?>