我有这个代码
$_SESSION['id'][] = $link;
这是正确的,结果就像这样
Array ( [id] => Array ( [0] => 4 [1] => 3 [2] => 2 [3] => 1 [4] => 4 [5] => 4 [6] => 1 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 ) )
我希望显示数组中的每个索引单
foreach($_SESSION as $key=>$value){
echo $key;
echo $value;
echo $_SESSION[$key][$value];
echo '<br />';
}
怎么可以?
id Notice: Array to string conversion in ajax\ajax.php on line 18
Array Warning: Illegal offset type in ajax\ajax.php on line 19
第18和19行
echo $value;
echo $_SESSION[$key][$value];
答案 0 :(得分:3)
循环遍历$ _SESSION ['id']数组而不是$ _SESSION数组
foreach($_SESSION[id] as $value){
echo $value;
echo '<br />';
}
答案 1 :(得分:3)
我认为你正在循环错误的数组级别,因为你需要获得id
只需更改为
foreach($_SESSION['id'] as $key=>$value){
echo "$key=>$val" . "<br>";
}
答案 2 :(得分:1)
foreach($_SESSION['id'] as $key=>$value){
echo $key;
echo $value;
echo "$key:$value";
echo '<br />';
}
答案 3 :(得分:0)
$ _ SESSION ['id']是一个数组。但是如果你想迭代$ _SESSION中的所有内容,我认为这应该有帮助!
foreach($_SESSION as $key => $value)
{
if(is_array($value))
{
foreach($value as $k => $v)
{
echo $key."[".$k."] = ".$v ;
}
}else{
echo $key." = ".$value;
}
}