Search code examples
phparraysunset

delete data from array


I have this issue with deleting a data from an array(session).

Well, my code is:

$cart = $this->session->userdata('cart');
$sess_product_name = $cart['product_name'];
$sess_prod_id = $this->input->post('sess_prod_id');

foreach($sess_product_name as $key => $value) :
    if($key == $sess_prod_id) :
        unset($sess_product_name[$value]);
        $sess_product_name = array_values($sess_product_name);
    endif;
endforeach;

Even so, the array is not changing in any way :( please help

Thank you very much for helping!


Solution

  • $value is the value associated with the key. If you wanted to remove that value, you would unset the associated key:

    unset($sess_product_name[$key]);