-3

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.

John
  • 227
  • 3
  • 16
  • 1
    https://en.wikipedia.org/wiki/JSON -> http://php.net/manual/en/function.json-decode.php – CBroe Aug 24 '17 at 08:58

4 Answers4

0

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'];
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

Use json_decode

$str = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
print_r(json_decode($str, true));

You need to study about JSON type and how to encode or decode to get the data.

Naincy
  • 2,953
  • 1
  • 12
  • 21
0

try this

echo "<pre>";
print_r( json_decode('{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}'));

to view the result , click run or hit f9, here

user10089632
  • 5,216
  • 1
  • 26
  • 34
0

try json_decode function

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

$data = json_decode($str, true);

print_r($data);

output:

Array ( [per_meter_en-GB] => TEST_FOR_TEST [roll_40_en-GB] => [ask_for_price_en-GB] => YES )

here you can access required value from $data array eg. echo $data['per_meter_en-GB']; will output TEST_FOR_TEST

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44