I have this json string:
$row['medium'] = "{\"medium\":13|17|1|14, \"medium_sub\":21}";
now I want to make an array from the “medium” part:
$medium_arr = $row['medium']->{'medium'};
$medium_arr =
explode("|", $medium_arr);
print_r($medium_arr); //This is empty
when I make an array with zero’s like (but I know, this makes no sense)
$row['medium'] = "{\"medium\":1301701014, \"medium_sub\":21}";
$medium_arr = explode("0", $medium_arr);
print_r($medium_arr); //This gives 4 results
how is it possible that I can not use the explode in a proper way?
If that is your real JSON string, it isn’t valid. The pipe-delimited string must be quoted. Using zeros worked because it resulted in a numeric value for
mediumthat didn’t need quotes.It can then be decoded properly with
json_decode().Update
I would add that instead of using a delimited string, if you have the ability to modify the JSON, you ought to use an array for those values in the JSON as: