PHP中如何循环输出数组?

$list[] = array('upid'=>'1','title'=>'公司新闻','list'=>array('id'=>'3','title'=>'公司新闻标题测试!','intime'=>'2009-29-5'));
foreach ($list as $a){
echo $a['upid'] .'-'. $a['title'];
foreach ($a['list'] as $b){
echo $b['title'];
}
}
这样写对嘛?
为什么输出不对呢?
最新回答
靉哥哥旳文

2024-10-30 14:33:11

$list = array('upid'=>'1','title'=>'公司新闻','list'=>array('id'=>'3','title'=>'公司新闻标题测试!','intime'=>'2009-29-5'));
foreach ($list as $v=>$a)
{
/*echo $a['upid'] .'-'. $a['title'];
foreach ($a['list'] as $b){
echo $b['title'];
}*/

if($v=='list')
{
foreach ($a as $b)
{
echo $b;
}
}
else
{
echo $a;
}
}

这样就行了..
顾北凉城

2024-10-30 11:30:48

$list = array('upid'=>'1','title'=>'公司新闻','list'=>array('id'=>'3','title'=>'公司新闻标题测试!','intime'=>'2009-29-5'));
foreach ($list as $v=>$a)
{

if($v=='list')
{
foreach ($a as $b)
{
echo $b;
}
}
else
{
echo $a;
}
} 不一定是最好的.但是可以运行.好好理解喔