PHP session数组输出(遍历输出)
做购物车弄的下边是存储的sessionvar_dump的结果,我想遍历数组输出里边的信息,如编号名称价格等array(size=3)'C00000013'=>array(...
做购物车弄的下边是存储的session var_dump的结果,我想遍历数组输出里边的信息,如编号 名称 价格等
array (size=3)
'C00000013' =>
array (size=5)
'id' => string 'C00000013' (length=9)
'name' => string '醇香鲜米' (length=22)
'price' => string '6.85' (length=4)
'num' => int 70
'img' => string '5kg' (length=3)
'C00000016' =>
array (size=5)
'id' => string 'C00000016' (length=9)
'name' => string '典香鲜米' (length=22)
'price' => string '8.75' (length=4)
'num' => string '2' (length=1)
'img' => string '10kg' (length=4)
'C00000018' =>
array (size=5)
'id' => string 'C00000018' (length=9)
'name' => string '鲜米卡' (length=19)
'price' => string '92' (length=2)
'num' => int 3
'img' => string '典香' (length=6) 展开
array (size=3)
'C00000013' =>
array (size=5)
'id' => string 'C00000013' (length=9)
'name' => string '醇香鲜米' (length=22)
'price' => string '6.85' (length=4)
'num' => int 70
'img' => string '5kg' (length=3)
'C00000016' =>
array (size=5)
'id' => string 'C00000016' (length=9)
'name' => string '典香鲜米' (length=22)
'price' => string '8.75' (length=4)
'num' => string '2' (length=1)
'img' => string '10kg' (length=4)
'C00000018' =>
array (size=5)
'id' => string 'C00000018' (length=9)
'name' => string '鲜米卡' (length=19)
'price' => string '92' (length=2)
'num' => int 3
'img' => string '典香' (length=6) 展开
3个回答
推荐于2018-04-26
展开全部
<?php//你的数组语法不对,我修改了一下,递归调用traversalMap()函数就能遍历各值
$b=array (
'C00000013' =>
array (
'id' => 'C00000013',
'name' => '醇香鲜米' ,
'price' => '6.85',
'num' => 70,
'img' => '5kg'),
'C00000016' =>
array (
'id' => 'C00000016',
'name' => '典香鲜米',
'price' => '8.75' ,
'num' => '2' ,
'img' => '10kg'),
'C00000018' =>
array (
'id' =>'C00000018',
'name' => '鲜米卡',
'price' => '92',
'num' => 3,
'img' => '典香'));
function traversalMap($arr)
{
foreach($arr as $k=>$v)
{
if(!is_array($v))
echo $k."=".$v."<BR>";
else
echo $k."<br>";
if(is_array($v))
traversalMap($v);
}
}
traversalMap($b);
?>
$b=array (
'C00000013' =>
array (
'id' => 'C00000013',
'name' => '醇香鲜米' ,
'price' => '6.85',
'num' => 70,
'img' => '5kg'),
'C00000016' =>
array (
'id' => 'C00000016',
'name' => '典香鲜米',
'price' => '8.75' ,
'num' => '2' ,
'img' => '10kg'),
'C00000018' =>
array (
'id' =>'C00000018',
'name' => '鲜米卡',
'price' => '92',
'num' => 3,
'img' => '典香'));
function traversalMap($arr)
{
foreach($arr as $k=>$v)
{
if(!is_array($v))
echo $k."=".$v."<BR>";
else
echo $k."<br>";
if(is_array($v))
traversalMap($v);
}
}
traversalMap($b);
?>
展开全部
foreach($arr as $v){
echo '编号:'.$v['id'].'<hr>';
echo '名称:'.$v['name'].'<hr>';
echo '价格:'.$v['price'].'<hr>';
}
变量后面输出的是换行符,不知道为什么百度这里显示不出来!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
foreach($_SESSION['arr'] as $v)//arr为你要输出的内容
{
print_r($v);
}
{
print_r($v);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询