如何在PHP中将字符串转换为数组?

时间:2018-07-28 14:18:33

标签: php arrays string

我有一个像波纹管这样的字符串:

Asia:JP,America:US,Africa:LY  ...

所以我想将其转换为这样的数组:

array("Asia"=>"JP","America"=>"US","Africa"=>"LY");

那该怎么做呢?谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用typedef const struct S { int a; int b; } s; // The wrong way s thisIsAnS; thisIsAnS.a = 1; //error: assignment of member ‘a’ in read-only object // The correct way s thisIsAnS = { 1 , 2 }; //compiles fine, but you can not change a or b anymore

explode

答案 1 :(得分:0)

$contury = "Asia:JP,America:US,Africa:LY";

//Replace commas to space
$str1 = str_replace(","," ",$contury);

//Replace colon to commas
$str2 = str_replace(":",",",$str1);

//Replace space to commas
$str3 = str_replace(" ",",",$str2);

//Convert string to array
$ar = explode(",",$str3);

echo $ar[0];