转换 PHP 数组


Convert PHP array

我有像这样的php数组:

['AL'=>'Albania','AD'=>'Andorra','AT'=>'Austria']

我需要将其转换为

[[code=>'AL',country=>'Albania'],[code=>'AD',country=>'Andorra'],[code=>'AT',country=>'Austria']].

如何在 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);
?>

输出:

Array ( [0] => Array ( [code] => AL [country] => Albania ) [1] => Array ( [code] => AD [country] => Andorra ) [2] => Array ( [code] => AT [country] => Austria ) )

您需要的 PHP 脚本和输出表明您必须使用关联数组。 这些数组允许在数组中使用命名键。只需使用以下代码即可获取所需的输出:

<?php
    $a = ['AL'=>'Albania','AD'=>'Andorra','AT'=>'Austria'];//Associative Array Declaration
    $output = array();
    foreach($a as $w => $x)//For every value in $a, it will display code and the country
        $output[] = array("code" => $w, "country" => $x);
    print_r($output);//Displaying the array output
?>