我将$ _SESSION [' items']存储为购物车商品,结构如下:
的print_r($ _ SESSION ['项目']):
Array
(
[0] => Array
(
[p_name] => Germany Fully Synthetic Engine Oil 5w40, 4L
[p_code] => 15177651
[p_coverImg] => 13-1460446338-kMTXa.jpg
[p_id] => 13
[p_price] => 126.00
[p_qty] => 2
)
[1] => Array
(
[p_name] => BetterBody Foods Organic Cold Pressed Extra Virgin Coconut Oil
[p_code] => 15599414
[p_coverImg] => 9-1460445708-6XlfS.jpg
[p_id] => 9
[p_price] => 278.40
[p_qty] => 5
)
[2] => Array
(
[p_name] => X-Dot Motorbike Helmet G88 + Bogo Visor (Tinted)
[p_code] => 2102649
[p_coverImg] => 12-1460446199-wI5qx.png
[p_id] => 12
[p_price] => 68.00
[p_alt-variation-1] => Blue
[p_alt-variation-2] => L
[p_qty] => 2
)
)
所以删除项目我使用unset($_SESSION['items'][$arr_key]);
从上面的数组中我说unset($_SESSION['items'][0]);
,在所有I print_r($_SESSION['items'])
之后,数组键不再以[0]开头:
Array
(
[1] => Array
(
[p_name] => BetterBody Foods Organic Cold Pressed Extra Virgin Coconut Oil
[p_code] => 15599414
[p_coverImg] => 9-1460445708-6XlfS.jpg
[p_id] => 9
[p_price] => 278.40
[p_qty] => 5
)
[2] => Array
(
[p_name] => Wonder Belt Set (2pcs)
[p_code] => 33134567
[p_coverImg] => 2-1460445193-vZwGV.jpg
[p_id] => 2
[p_price] => 199.00
[p_qty] => 1
)
)
如何在删除某个项目后再次使用键0
恢复这些会话数组?
答案 0 :(得分:0)
建议您使用array_values()来求助键。一个例子:
$array = [1, 2, 3];
unset($array[0]);
print "<pre>";
print_r($array);
print "</pre>";
<强>输出:强>
Array
(
[1] => 2
[2] => 3
)
// Resort keys using array_values
$array = array_values($array);
print "<pre>";
print_r($array);
print "</pre>";
使用array_values后的输出:
Array
(
[0] => 2
[1] => 3
)
答案 1 :(得分:0)
使用array_values()
array_values() returns all the values from the array and indexes the array numerically.
答案 2 :(得分:0)
您应该使用array_values作为
array_values($_SESSION['items']);