php内爆数组元素

时间:2013-10-11 23:38:02

标签: php arrays string

如何打开以下数组

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

进入以下输出?请注意,每个数组元素都在一个单独的行中:

1:a 
2:b
3:c

2 个答案:

答案 0 :(得分:4)

您正在寻找basic for-each construct

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

答案 1 :(得分:1)

var $colon_separated = array();
foreach ($a as $key => $value) {
    $colon_separated[] = "$key:$value";
}
$result = implode("\n", $colon_separated);