2015-01-18 1 views
-3

나는이 같은 PHP 배열 :변환 PHP 배열

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

그리고 내가 PHP에서이 작업을 수행하는 방법

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

로 변환 할 필요가?

+0

이 원래의 배열을 통해 루프를 사용하여, 당신은 ( –

+0

사용 [foreach 문] 원하는 http://php.net를 구조에 새로운 배열을 구축 /manual/it/control-structures.foreach.php)를 사용하여 데이터를 반복합니다. –

+0

아직 보지 않았다면, 여기 사이트를 방문해보십시오. http://stackoverflow.com/tour – Rizier123

답변

5

이 당신을 위해 작동합니다 :

<?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)) 
+0

답변 해 주셔서 감사합니다. – DSG

0

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 

?>