PHP Question

How to get names and values of cookies from PHP

I'm coding an e-commerce website that uses cookies to transfer information from the product page to the cart.

$_COOKIE
has all of the cookies for a certain page put into an array, with the name of the cookie and the the value of the cookie. Example:

array(2) { ["0_USB_Keyboard"]=> string(6) "$19.99" ["0_Alarm_Clock_Radio"]=> string(6) "$14.99" }


How can I take each cookie name and put them in an new array, so they can be used elsewhere. And how can I put each cookie value in an new array, so that they too can be used else where?

Answer

// Edit
Use array_keys and array_values
Example:

$array = array( 
 "0_USB_Keyboard" => "$19.99", 
 "0_Alarm_Clock_Radio" => "$14.99"
);

print_r(array_keys($array));
print_r(array_values($array));

Output would be:

Array
(
    [0] => 0_USB_Keyboard
    [1] => 0_Alarm_Clock_Radio
)
Array
(
    [0] => $19.99
    [1] => $14.99
)

I hope this helps you.

Comments