John John - 1 month ago 10
PHP Question

How to access data from {string}

Some fields save data in the following way in one of my database field:


{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}


(* Don't know how it's called so i wrote string in the title)

How can i access data in particular for every "value".
One way could be to explode for every field but is there a better way to do this?

$ask_for_price_variable = [value from field];
if ($ask_for_price_variable == 'YES') {
// Do something
}


EDIT: As i said i did not know how it was called "JSON" so i could not search for it. Thank you all for the answers.

Answer Source

It is json data. You can access it using json_decode in php

$json = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';

$data = json_decode($json,true);
$ask_for_price_variable = $data['ask_for_price_en-GB'];