-3

I have a column of datatype array in sql like:

enter image description here

This value is received as :

$customlabel = "{new,\"tree test123\",de}"

I tried json_decode($customlabel, TRUE) to get it into array. But this just returns empty string

enter image description here

Azima
  • 3,835
  • 15
  • 49
  • 95
  • 4
    This simply isn't valid JSON. First of all, curly braces means object, not array - so you'd need key-value pairs, not just simple values. And `new` and `de` would have to be in double quotes. – CBroe Nov 10 '22 at 06:58
  • 1
    Please post code enclosed in appropriate tags and no images! – jspit Nov 10 '22 at 07:03
  • 1
    _"But this just returns empty string"_ - It actually returns `null`, and as it says in the manual: "null is returned if the json cannot be decoded". Use `var_dump()` instead of `print_r()` when debugging. `var_dump()` will show you the actual data and data types. – M. Eriksson Nov 10 '22 at 07:11

1 Answers1

1

This is not valid JSON. You can not decode the JSON. So you can consider it as a string and my answer would be..

$customlabel = html_entity_decode("{new,\"tree test123\",de}");
$customlabel = str_replace(array('{', '}'), '', $customlabel);
$customlabel = explode(',', $customlabel);
$customlabel = array_map('strval', $customlabel);
print_r($customlabel);

Your output will be...

Array ( [0] => new [1] => "tree test123" [2] => de )
Avinash
  • 137
  • 8