How to add extra field to php json array

This is what i am trying to achieve:

{
“operationID”:"",
“data”: [
{
“passengerName”: “”,
“passengerAddress”: “”,
},
{
“passengerName”: “”,
“passengerAddress”: “”,
},
{
“passengerName”: “”,
“passengerAddress”: “”,
}
]
}

This is what i have currently:

{
“data”: [
{
“passengerName”: “”,
“passengerAddress”: “”,
},
{
“passengerName”: “”,
“passengerAddress”: “”,
},
{
“passengerName”: “”,
“passengerAddress”: “”,
}
]
}

So as you can see i’m trying to add the extra field “operationID”:"" before data

first decode the json to an php associative array

 $array = json_decode($json, true);

echo '<pre>' . print_r($array, true) . '</pre>';

then you can add elements to the array

$array['something'] = 'foo';

and encode it to json again

$json = json_encode($array);

note the sequence of an associative array does not matter at all

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service