如何通过键和值名称调用会话值

时间:2014-08-14 09:21:00

标签: php arrays session

以下是我定义的会话:

$_SESSION['addons'][] = array(
   'addon'    => $addon_id,
   'quantity' => $qty,
   'date_a'   => $date_1,
   'date_b'   => $date_2,
   'car_id'   => $car_id,
   'pickup'   => $location,
   'drop'     => $d_location
);

我这样称呼它:

<?php
foreach($_SESSION['addons'] as $addon=>$add_val)
{
    //echo $addon;
    foreach($add_val as $value)
    {
        echo $value;
    }
}
?>

目前它显示所有值并且值正确显示但我希望它们通过我在会话中专门设置的名称来调用。例如,

$value['addon'];

这样我就可以选择要调用的值和不调用的值。

我试过这样的事情:

//setting adon id as key and the rest as value  but doesn't work in the way I wanted.
$_SESSION['addons'][$addon_id] = array(
   'quantity' => $qty,
   'date_a'   => $date_1,
   'date_b'   => $date_2,
   'car_id'   => $car_id,
   'pickup'   => $location,
   'drop'     => $d_location
);

2 个答案:

答案 0 :(得分:0)

我猜你希望直接看到特定键的值。您可以初始化会话,如:

$_SESSION['addons'] = array('addon'=>$addon_id,'quantity'=>$qty,'date_a'=>$date_1,'date_b'=>$date_2,'car_id'=>$car_id,'pickup'=>$location,'drop'=>$d_location);

然后例如,如果您希望回显键的值&#34; date_a&#34;你可以这样做:

echo $_SESSION['addons']['date_a'];

希望这有帮助!

答案 1 :(得分:0)

在您的代码中,add_val是每个插件的数组,因此echo add_val['addon'];将包含$addon_id

你有去过的地方,你只是走得太深了