使用php将新字段和数据添加到json编码对象并删除引号

I converted php array to json and I try to add another field and data with quotes removed. this is json object generated

$data_string = json_encode($data);

It outputs this .

{"dateDebut":"36000000","dateFin":"45000000","periodeDebut":"1410818400","periodeFin":"1411596000","jours":"Thursday","role":{"idRole":"1"},"zone":{"idzone":"Z1E2"},"tag":{"id":"511651969251"},"typeNotification":{"typeNotif":"Alerte"}}

I tried this

$data_string['message']=1;

and it outputs this wrong object with the "1" in the beginning

1"dateDebut":"36000000","dateFin":"45000000","periodeDebut":"1410818400","periodeFin":"1411596000","jours":"Thursday","role":{"idRole":"1"},"zone":{"idzone":"Z1E2"},"tag":{"id":"511651969251"},"typeNotification":{"typeNotif":"Alerte"}}

even adding the field with quotes like this

$data_string['message']="1";

doesn't add the field message in the generated object json at all.

You cant add data to the json string, because its a string.

Add the data before you json encode it:

$data['message']=1;
$data_string = json_encode($data);

Or if the original php object $data is out of scope by this point, you must decode to php object, add data and then encode back to json:

$data = json_decode($data_string);
$data['message']=1;
$data_string = json_encode($data);

YOu can do this:

$data_array = json_decode($data_string);
$data_array['message'] = 1;
$data_string = json_encode($data_array);

The string $data_string should contain the new member message with value 1;