Home > Software engineering >  Get last car details from cookies
Get last car details from cookies

Time:11-16

We have car details saved in cookie

$_COOKIE['ymm_selected'] = {\"vehicle\":\"Alfa Romeo,Giulia,2022\",\"vehicles\":[\"Citroen,DS3,2018\",\"Citroen,C2,2006\",\"Alfa Romeo,Giulia,2022\"]}

I want to get a variable for current vehicle which is always the last one, in this case it is Alfa Romeo Giulia 2022

I tried the following:

    $cookiev = $_COOKIE['ymm_selected'];
    $vehiclef1 = get_string_between($cookiev, 'vehicles\":[\"', '\"]}');
    $vehiclef2  = preg_replace('/,[^,]*$/', '', $vehiclef1);
    $vehiclef3 =  preg_replace('/[ ,] /', ' ', trim($vehiclef2));

This will give the following

Citroen DS3 2018" "Citroen C2 2006" "Alfa Romeo Giulia

I want to get the last vehicle. The vehicle is different every time so I cannot do that by counting.

Your help is much appreciated

CodePudding user response:

something like that :

$_COOKIE['ymm_selected'] = '{\"vehicle\":\"Alfa Romeo,Giulia,2022\",\"vehicles\":[\"Citroen,DS3,2018\",\"Citroen,C2,2006\",\"Alfa Romeo,Giulia,2022\"]}';


$formatted = stripslashes($_COOKIE['ymm_selected']);

$decoded = json_decode($formatted, true);

echo end($decoded['vehicles']);

Regards,

  • Related