I have to parse this JSON in PHP so that for each address, I get the address, line and city information and store it in the database. The way that I plan to implement is like this:
For each key in the json string, check if it begins with address,
If yes, split the string based on '_' and get the index count.
Get line1, line2, city for this index count.
Is there a better way to do this?
(Note that the index count can be random)
{
"route": "A",
"address_0": "A0",
"line1_0": "L1_0",
"line2_0": "L2_0",
"city_0": "city_0",
"address_1": "A1",
"line1_1": "L1_1",
"line2_1": "L2_1",
"city_1": "city_1",
"address_2": "A2",
"line1_2": "L1_2",
"line2_2": "L2_2",
"city_2": "city_2",
}
Just iterate over the data and assign to an array:
gives:
Demo