How do I explode the following string:
$str = "ProductId=123, Name=Ancient Roots, Modern Pursuits, Country=India, City=Bangalore, Price=3368"
Such that the output array will contain:
[
"ProductId" => "123",
"Name" => "Ancient Roots, Modern Pursuits",
"Country" => "India",
"City" => "Bangalore",
"Price" => "3368"
]
I tried to explode by "comma", then each element again explode by "equal to" as.
$arr = explode(",", $str);
and again
$prodarr = explode("=", $arr[0]);
$product["ProductId"] = $prodarr[1]
But I'm facing the problem when another comma exists in a value like in name Ancient Roots, Modern Pursuits.
=is always a single word without commas or spaces? Also, if you have control over it, can't you simply switch to an established format like JSON?