-2

如何打开以下数组

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

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

1:a 
2:b
3:c
4

2 回答 2

4

您正在寻找基本的 for-each 构造

$p = array('1' => 'a', '2' => 'b', '3' => 'c');
foreach($p as $key => $value) {
  echo $key.":".$value."\n";
}
于 2013-10-11T23:40:27.633 回答
1
var $colon_separated = array();
foreach ($a as $key => $value) {
    $colon_separated[] = "$key:$value";
}
$result = implode("\n", $colon_separated);
于 2013-10-11T23:42:28.050 回答