php foreach遍历的问题,很简单 可是我忘了
<?php$arr=array(array("id"=>1,"name"=>"tom","age"=>35,"xl"=>"大学","address"=>"北京"),arr...
<?php
$arr=array(
array("id"=>1,"name"=>"tom","age"=>35,"xl"=>"大学","address"=>"北京"),
array("id"=>2,"name"=>"jim","age"=>23,"xl"=>"初中","address"=>"上海"),
array("id"=>3,"name"=>"lili","age"=>26,"xl"=>"硕士","address"=>"四川"),
array("id"=>4,"name"=>"wom","age"=>54,"xl"=>"博士","address"=>"安徽")
);
?>
用foreach遍历出来,该怎么写。求大神帮助???不胜感激 最好写明思路
写的明白追加悬赏 展开
$arr=array(
array("id"=>1,"name"=>"tom","age"=>35,"xl"=>"大学","address"=>"北京"),
array("id"=>2,"name"=>"jim","age"=>23,"xl"=>"初中","address"=>"上海"),
array("id"=>3,"name"=>"lili","age"=>26,"xl"=>"硕士","address"=>"四川"),
array("id"=>4,"name"=>"wom","age"=>54,"xl"=>"博士","address"=>"安徽")
);
?>
用foreach遍历出来,该怎么写。求大神帮助???不胜感激 最好写明思路
写的明白追加悬赏 展开
1个回答
展开全部
<?php
$arr = array(
array("id"=>1,"name"=>"tom","age"=>35,"xl"=>"大学","address"=>"北京"),
array("id"=>2,"name"=>"jim","age"=>23,"xl"=>"初中","address"=>"上海"),
array("id"=>3,"name"=>"lili","age"=>26,"xl"=>"硕士","address"=>"四川"),
array("id"=>4,"name"=>"wom","age"=>54,"xl"=>"博士","address"=>"安徽")
);
foreach( $arr as $key => $value )
{
echo $value['id'] . '-' . $value['name'] . '-' . $value['age'] . '-' . $value['xl'] . '-' . $value['address'] .PHP_EOL;
}
/* return
1-tom-35-大学-北京
2-jim-23-初中-上海
3-lili-26-硕士-四川
4-wom-54-博士-安徽
*/
循环拿出来,之前是一个集合,现在是单独一条记录,然后根据key就能拿到当然记录的结果。
追问
貌似不是这样写的。是用的两个foreach 嵌套
追答
不知道你是怎么考虑的,但是一般来说,能一次拿出来的数据,就不要去做两遍操作。
<?php
$arr = array(
array("id"=>1,"name"=>"tom","age"=>35,"xl"=>"大学","address"=>"北京"),
array("id"=>2,"name"=>"jim","age"=>23,"xl"=>"初中","address"=>"上海"),
array("id"=>3,"name"=>"lili","age"=>26,"xl"=>"硕士","address"=>"四川"),
array("id"=>4,"name"=>"wom","age"=>54,"xl"=>"博士","address"=>"安徽")
);
foreach( $arr as $key => $value )
{
foreach( $value as $val)
{
echo $val .PHP_EOL;
}
}
//RETURN 1 tom 35 大学 北京 2 jim 23 初中 上海 3 lili 26 硕士 四川 4 wom 54 博士 安徽
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询