1
我正在使用CodeIgniter
編寫此項目,您可以在其中添加和刪除項目,例如網上商店。從會話信息中刪除項目
我在我的會話類中有一個數組,我試圖做的是按id搜索元素並刪除它們。
public function removeRow($id) {
if ($id) {
$bucket = $this->session->userdata('bucket');
foreach ($bucket as $key => $value) {
if ($value['id'] == $id) {
unset($bucket[$key]);
}
}
$this->session->set_userdata(array(
'bucket' => $bucket
)
);
}
和session
信息:
Array
(
[session_id] => e0c6303a7c24a05436ef0abfe2424c44
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0
[last_activity] => 1363718778
[user_data] =>
[a] => test
[bucket] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 3
[name] => Maksas Frajus - Amžinybės glėbyje
[price] => 30.99
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[name] => Maksas Frajus - Atėjūnas
[price] => 35.99
)
)
[2] => Array
(
[0] => Array
(
[id] => 2
[name] => Maksas Frajus - Paprasti stebuklingi daiktai
[price] => 27.5
)
)
)
)
我收到此錯誤信息:
Message: Undefined index: id
,我甚至不知道我在做什麼錯。有人可以解釋嗎?
多麼愚蠢我 – Benedictus 2013-03-19 19:05:35
的看你的陣列,我不明白爲什麼你有額外的水平。您的數組應該採用上面顯示的格式。然後做一個簡單的'if(isset($ bucket [$ id])){unset($ bucket [$ id]);}'就足夠了,而不需要遍歷數組。 – 2013-03-19 19:14:48