Reputation: 4279
I am writing this project with CodeIgniter
, where you can add and remove items from your bucket, like an internet shop.
I have an array inside my session class, what I'm trying to do is to search the elements by id and remove them.
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
)
);
}
and the session
information:
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
)
)
)
)
i get this error message :
Message: Undefined index: id
and i don't even know what I'm doing wrong. could someone explain?
Upvotes: 1
Views: 98
Reputation: 15045
if ($value['id'] == $id) {
should be this:
if ($value[0]['id'] == $id) {
You have a nested array so you need to go one level deeper.
Example Array per comment below:
[bucket] => Array
(
[3] => Array
(
[id] => 3
[name] => Maksas Frajus - Amžinybės glėbyje
[price] => 30.99
)
[1] => Array
(
[id] => 1
[name] => Maksas Frajus - Atėjūnas
[price] => 35.99
)
[2] => Array
(
[id] => 2
[name] => Maksas Frajus - Paprasti stebuklingi daiktai
[price] => 27.5
)
)
Upvotes: 2