在数组中设置$ _cookie值

I have a cookie with the following value:

2,3 personen klein,47.50,images/portfolio/portfolio-02.jpg,1|1,2 personen
kleurrijk,47.50,images/portfolio/portfolio-01.jpg,1|3,Sneeuwklokje,47.50,images/portfolio/portfolio-03.jpg,1

I get this value when i use:
echo $_cookie['cart'];

The | is the delimiter for a new line and the , is the delimiter for a new value.

How can I put these values in an array so it becomes useable to echo parts of it, for example when I only want to echo the images. Hope you guys can give me some tips.

$array = explode("|", $_COOKIE['cart']); 
foraech($array as & $element) {
   $element = explode(",", $element);
}

// example - echo an image
echo $array[1][3]

but to be honest you should keep it in $_SESSION. you can store arrays, objects, long strings etc. there and user is not able to change its contents

  • Cookies are not good for storing complex data

  • Everyone can change their cookies manually and it can lead to security issues

  • There is limit 4096 bytes per one cookie

I totally agree with Peter.

Cookies are simple text files. Do not store any Data like the above in Cookies! You have a nice container with the $_SESSION. It's a global associative array, so you can store values like $_SESSION['cart'].

Put in there whatever u like!

Its more secure than a txt-file on the client's pc!

Read here about Sessions

    $string = "2,3 personen klein,47.50,images/portfolio/portfolio-02.jpg,1|1,2 personen kleurrijk,47.50,images/portfolio/portfolio-01.jpg,1|3,Sneeuwklokje,47.50,images/portfolio/portfolio-03.jpg,1";

    $lines = explode('|', $string);

    $lines = array_map(function($a){
        return explode(',', $a);
    }, $lines);

    print_R($lines);