I am receiving the json payload below from a webservice and I am trying to separate it into 3 different arrays using loops after decoding whlist keeping the same array keys:
{
"id": 5705376,
"title": "Satellite Installation",
"created_at": "2017-09-07T14:02:19.000Z",
"updated_at": "2017-09-07T14:02:19.000Z",
"customer_id": 3126803,
"way_points": [{
"id": 7405587,
"lat": -26.0578251,
"lng": 28.02189520000002,
"task_id": 5705376,
"done": false,
"allow_editing_inventory": true,
"customer_contact_ids": [3126803],
"customer": {
"original_phone_number": null,
"last_open_at": null,
"last_order_at": null,
"uploaded_profile_image": {
"url": "/images/avatar.png"
},
"original_lat": null,
"original_lng": null,
"district": null,
"house_number": null,
"street": null
},
"full_address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa"
}],
"customer": {
"id": 3126803,
"name": "Lance",
"address": "8 Anslow Ln, Bryanston, Sandton, 2191, South Africa",
"lat": -26.0578251,
"lng": 28.0218952,
"created_at": "2017-08-29T10:00:32.360Z",
"updated_at": "2017-09-07T14:02:19.860Z",
"phone": null,
"merchant_id": 11221,
"allow_login": false,
"stripe_id": null,
"original_phone_number": null,
"last_open_at": null,
"last_order_at": null,
"uploaded_profile_image": {
"url": "/images/avatar.png"
},
"original_lat": null,
"original_lng": null,
"house_number": null,
"street": null
},
"late": false,
"external_id": "5705376",
"uuid": "28095e33-b30c-4e35-98d2-aae6ad428f66",
"dispatcher_id": null,
"team_ids": [12422],
"ready_to_execute": true,
"tip_driver_enabled": false,
"automatically_assigned": false,
"task_inventories": null,
"task_notes": null
}
First you'll need to decode the json encoded string into a usable format, either array
or an object. You can find all possible options for json_decode
here.
$inputArray = json_decode($inputString, true);
Now for the loop. To keep it simple, you can initialize the three arrays you need firsthand as empty arrays. Then, in the loop, simply check for the key value and choose what array to append the data to.
$waypoints = array();
$customers = array();
$orders = array();
$count = 0;
foreach($inputArray as $key => $element) {
/* using if statements */
if($key === 'way_points') {
$waypoints[] = $element;
}
elseif($key === 'customer') {
$customers[] = $element;
}
else {
$orders[$count][$key] = $element;
}
/* using a switch/case */
switch($key) {
case 'way_points':
$waypoints[] = $element;
break;
case 'customer':
$customers[] = $element;
break;
default:
$orders[$count][$key] = $element;
break;
}
$count++;
}
At this point you should have all the data you need, but we still haven't changed the keys. You could leave it as-is, after all it is pretty self-explanatory that the id
key in $waypoints
reprensents the waypoint_id
. However, if you do absolutely need to change the key, there's a few ways to do this. You can loop over the newly formed $waypoints
array and modify the keys in this new loop.
$waypoints = change_keys($waypoints);
function change_keys($arr) {
return array_map(function($waypoint) {
return array(
'waypoint_id' => $waypoint['id'],
'lat' => $element['lat'],
/* remaining fields */
);
}, $arr);
}
Or you can cut the amount of steps and do it in the initial foreach
loop
$waypoints = array();
$customers = array();
$orders = array();
$count = 0;
foreach($inputArray as $key => $element) {
if($key === 'way_points') {
$waypoints[] = array_map(function($element) {
return array(
'waypoint_id' => $element['id'],
'lat' => $element['lat'],
/* remaining fields */
);
}, $arr);
}
/* ... */
}
You can find more info on array_map
here.
As an aside, as @jeroen mentionned in the comments, you can simply use json_decode
and you'll be left with a usable associative array. to loop over the waypoints for example, you would simply write foreach($myArray[waypoints] as $key => $waypoint)
.