php - Not able to retrieve cookie data from a multidimensional array -


this question has answer here:

i'm new cookies here how i'm setting , retrieving data

if(!isset($_cookie['cart'])){ $_cookie['cart'] = array(); }  setcookie("cart[$stk_id]['name']", $name, time()+24*60*60, "/"); setcookie("cart[$stk_id]['quantity']", $qty, time()+24*60*60, "/"); setcookie("cart[$stk_id]['vendor']", $vendor, time()+24*60*60, "/");  foreach ($_cookie['cart'] $stk_id => $product){     $qty = $product['quantity'];     $pro_name = $product['name']; }  

but i'm getting error notice: undefined index: quantity , name. problem?

cookies array store commonly used once variable ,you set cookies name in array,but create array first , after set cookie name more convenient.here can set json_encode , json_decode

$data = array($stk_id =>                array(                    "name" => $name,                    "quantity" => $qty,                    "vendor" => $vendor                    )              ); setcookie("cart", json_encode($data), time()+24*60*60, "/");    $cookie = json_decode($_cookie['cart'],true);//for array output  foreach ($cookie $stk_id => $product){             $qty = $product['quantity'];             $pro_name = $product['name']; }  

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -