2013-10-11 78 views
-2

如何打開下面的數組PHP破滅數組元素

$p = array('1' => 'a', '2' => 'b', '3' => 'c'); 

到下面的輸出?需要注意的是每個數組元素是在單獨一行:

1:a 
2:b 
3:c 

回答

4

您正在尋找的basic for-each construct

$p = array('1' => 'a', '2' => 'b', '3' => 'c'); 
foreach($p as $key => $value) { 
    echo $key.":".$value."\n"; 
} 
1
var $colon_separated = array(); 
foreach ($a as $key => $value) { 
    $colon_separated[] = "$key:$value"; 
} 
$result = implode("\n", $colon_separated);