我有陣列的列表,並需要他們輸出與printf語句通過與foreach語句陣列
<?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'兩次 - 第二個將在寫的第一個。這絕對沒有幫助。 – andrewsi