我只是在学习PHP而且我坚持这个问题。
使用foreach循环读取具有三个用户信息的数组中的元素
所以我到目前为止这样做了
$person = array (
"first_name" => "Carlos",
"last_name" => "Leon",
"address" => "630 west 139 street",
"city" => "New York",
"zip_code" => "11364"
);
foreach ($person as $attribute =>$data ){
$attr_nice =ucwords ($attribute);
$attr_nice =str_replace ("_" , " ", $attr_nice);
echo "$attr_nice = $data <br/>";
}
接下来是什么?我怎样才能做这三个用户?
答案 0 :(得分:1)
要创建嵌套数组,您需要以下内容:
$people = array(
array(
"first_name" => "Carlos",
"last_name" => "Leon",
"address" => "630 west 139 street",
"city" => "New York",
"zip_code" => "11364"
),
array(
"first_name" => "...",
"last_name" => "...",
"address" => "...",
"city" => "...",
"zip_code" => "..."
),
);
然后执行foreach ($people as $person) { }
,您的原始代码可以进入此循环。
答案 1 :(得分:0)
你也可以把$ person放在另一个数组中。