DSG*_*DSG -3 php associative-array
我有php数组像:
['AL'=>'Albania','AD'=>'Andorra','AT'=>'Austria']
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为
[[code=>'AL',country=>'Albania'],[code=>'AD',country=>'Andorra'],[code=>'AT',country=>'Austria']].
Run Code Online (Sandbox Code Playgroud)
如何在PHP中执行此操作?
这应该适合你:
<?php
$arr = ['AL'=>'Albania','AD'=>'Andorra','AT'=>'Austria'];
$result = array();
foreach($arr as $k => $v)
$result[] = array("code" => $k, "country" => $v);
print_r($result);
?>
Run Code Online (Sandbox Code Playgroud)
输出:
Array ( [0] => Array ( [code] => AL [country] => Albania ) [1] => Array ( [code] => AD [country] => Andorra ) [2] => Array ( [code] => AT [country] => Austria ) )
Run Code Online (Sandbox Code Playgroud)