如果我有这样的json数组:
{
"code": 1,
"data_question": [
{
"id_question": "9",
"id_user": "9",
"name": "name"
}
],
"last_page": 1,
"count_data": 8
}
我的问题是如何在data_question对象中添加一个元素,如下所示:
{
"code": 1,
"data_question": [
{
"id_question": "9",
"id_user": "9",
"name": "name"
"age":"12" <--------
}
],
"last_page": 1,
"count_data": 8
}
请看看这个screeshot: Image PHP Code Image JSON
感谢您的帮助:)
答案 0 :(得分:0)
这可能会对您有所帮助:
<?php
$txt = '{
"code": 1,
"data_question": [
{
"id_question": "9",
"id_user": "9",
"name": "name"
}
],
"last_page": 1,
"count_data": 8
}';
$data = json_decode($txt,true);
$data['data_question'][0]['age'] = 12;
echo json_encode($data,JSON_PRETTY_PRINT);
答案 1 :(得分:0)
请尝试:
$array["data_question"][0]["age"] = "12";
echo json_encode($array);