下面是存储在会话中的数组。它工作得很好。
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon);
?>
我这样打电话给他们,
foreach($_SESSION['cart'] as $total=>$id)
{
echo $total;
echo $id['addon'];
}
现在我想在上面的数组中添加另一个数组,以包含date_from
和date_to
插件项目($id['addon']
)。
我该如何做,然后根据addon_id
打电话给他们?
这是暂时在数组中存储插件项ID的部分:
'addon'=>$addon
EDITED
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,array('d_from'=>$date_from,'d_to'=>$date_to))
答案 0 :(得分:0)
foreach($_SESSION['cart'] as $total=>$id)
{
echo $total;
foreach( $id['addon'] as $key=>$value)
{
echo "$key - $value";
}
}
我认为日期已经在$ addon中了。如果不是,您只需将变量添加到您已有的数组中,例如
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,'dates'=> array('d_from'=>$date_from,'d_to'=>$date_to))
或
$dates = array('d_from'=>$date_from,'d_to'=>$date_to);
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,'dates'=> $dates)
在这种情况下,您必须在第一个代码示例中将'addon'替换为'dates'。
答案 1 :(得分:0)
这将为您提供所需的输出
foreach($_SESSION['cart'] as $total=>$id)
{
if ($id['addon'] == $wanted_addon_to_add_dates) {
$array['date_from'] = $date_from;
$array['date_to'] = $date_to;
$id[(whatever your want here)] = $array;
}
}
只是建议让你的代码更有意义。 它将为您提供类似
的数组 array ('var'=>value, ..., array ('addon'=>addon_id, 'date_from'=>date_from, 'date_to'=>date_to))
foreach($_SESSION['cart'] as $total=>$id)
{
if ($id['addon'] == $wanted_addon_to_add_dates) {
$array['date_from'] = $date_from;
$array['date_to'] = $date_to;
$array['addon'] = $id['addon'];
$id['addon'] = $array;
}
}