【发布时间】:2013-07-04 04:19:20
【问题描述】:
我有一个数组列表,需要用 printf 语句将它们输出
<?php
$example = array("first" => "Bob", "last" => "Smith", "address" => "123 Spruce st" );
$example = array("first" => "Sara", "last" => "Blask", "address" => "5678 Maple ct" );
foreach ($example as $key => $val) {
printf("<p>hello my name is %s %s and i live at %s</p>",$example['first'],$example['last'], $example['address']);
}
?>
上面只输出最后一个数组,我需要它遍历所有数组并使用提供的key => value 组合生成<p>。这只是一个简化的示例,因为输出的html 中的真实代码会更加复杂
我试过了
foreach ($example as $arr){
printf("<p>hello my name is %s %s and i live at %s</p>",$arr['first'],$arr['last'], $arr['address']);
}
但它只为每个key => value输出一个字符
【问题讨论】:
-
您声明了两次
$example- 第二次将覆盖第一次。那肯定没用。