Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在预先存在的json对象和给定json对象索引之后添加空json对象-Php_Php_Mysql_Json - Fatal编程技术网

在预先存在的json对象和给定json对象索引之后添加空json对象-Php

在预先存在的json对象和给定json对象索引之后添加空json对象-Php,php,mysql,json,Php,Mysql,Json,所以我有一个从mysql查询生成的JSON对象数组 JSON对象看起来像: [ {"field1": "value1", "field2": "value2"}, {"field1": "value3", "field2": "value4"}, {"field1": "value5", "field2": "value6"}, {"field1": "value7", "field2": "value8"} ] 现在我想在数组中的第三个JSON对象之后添加如下空对象:{} 预

所以我有一个从mysql查询生成的JSON对象数组

JSON对象看起来像:

[
  {"field1": "value1", "field2": "value2"},
  {"field1": "value3", "field2": "value4"},
  {"field1": "value5", "field2": "value6"},
  {"field1": "value7", "field2": "value8"}
]
现在我想在数组中的第三个JSON对象之后添加如下空对象:
{}

预期结果:

[
  {"field1": "value1", "field2": "value2"},
  {"field1": "value3", "field2": "value4"},
  {"field1": "value5", "field2": "value6"},
  {},
  {"field1": "value7", "field2": "value8"}
]
我可以循环遍历每个json对象,可以替换当前对象的值,但不知道如何添加一个全新的json空对象

我的代码 ($j=0;$j<$length;$j++)的
{
如果($j==1){
//这里我不想替换Jth JSON Obj,而是添加新的空Obj,比如$data.push(“{}”)
$data[$j]['field1']='';
$data[$j]['field2']='';
}否则{
$data[$j]['question']=$ques_arr[$j];
$data[$j]['response']=$allresponses[$j]['response'];
}
}

例如,尝试通过
new\StdClass()
[(对象)[]]]
添加它

$test = '[
{"field1": "value1", "field2": "value2"},
{"field1": "value3", "field2": "value4"},
{"field1": "value5", "field2": "value6"},
{"field1": "value7", "field2": "value8"}
]';
 $temp = json_decode($test);
 array_splice($temp, 3, 0, new \StdClass());
$result = json_encode($temp);
var_dump($result);
返回
[{“field1”:“value1”,“field2”:“value2”},{“field1”:“value3”,“field2”:“value4”},{},{“field1”:“value7”,“field2”:“value8”}]

更新 使用并尝试:

    $test = '[
{"field1": "value1", "field2": "value2"},
{"field1": "value3", "field2": "value4"},
{"field1": "value5", "field2": "value6"},
{"field1": "value7", "field2": "value8"}
]';
    $temp = json_decode($test);
    $value = (new \StdClass());
    array_splice($temp, 3, 0, [(object)[]]);
    $result = json_encode($temp,true);
    var_dump($result);

根据您的需要。

尝试通过
new\StdClass()
[(对象)[]]
添加它

$test = '[
{"field1": "value1", "field2": "value2"},
{"field1": "value3", "field2": "value4"},
{"field1": "value5", "field2": "value6"},
{"field1": "value7", "field2": "value8"}
]';
 $temp = json_decode($test);
 array_splice($temp, 3, 0, new \StdClass());
$result = json_encode($temp);
var_dump($result);
    $json = '[
    {"field1": "value1", "field2": "value2"},
    {"field1": "value3", "field2": "value4"},
    {"field1": "value5", "field2": "value6"},
    {"field1": "value7", "field2": "value8"}
    ]';
    $temp = json_decode($json);
    $inserted = array( (object)[]);
    array_splice( $temp, 3, 0, $inserted );
    $result = json_encode($temp);
    var_dump($result);
返回
[{“field1”:“value1”,“field2”:“value2”},{“field1”:“value3”,“field2”:“value4”},{},{“field1”:“value7”,“field2”:“value8”}]

更新 使用并尝试:

    $test = '[
{"field1": "value1", "field2": "value2"},
{"field1": "value3", "field2": "value4"},
{"field1": "value5", "field2": "value6"},
{"field1": "value7", "field2": "value8"}
]';
    $temp = json_decode($test);
    $value = (new \StdClass());
    array_splice($temp, 3, 0, [(object)[]]);
    $result = json_encode($temp,true);
    var_dump($result);

你可以演示一个例子说明你所说的
stdClass()
array\u-splice($data,$j,0,new-stdClass())
这将在$jtry
array\u-splice
new-stdClass()插入一个空对象
@Fei你能举例说明一下你所说的
stdClass()
array\u拼接($data,$j,0,new stdClass())
这将在$j0处插入一个空对象。你已经将第三个Json对象替换为空{}。我不想替换它,但要添加一个新的object@MurlidharFichadia我解决了它,请再次检查我的答案。您已将第三个Json对象替换为空{}。我不想替换它,但要添加一个新的object@MurlidharFichadia我解决了,请再次检查我的答案。
    $json = '[
    {"field1": "value1", "field2": "value2"},
    {"field1": "value3", "field2": "value4"},
    {"field1": "value5", "field2": "value6"},
    {"field1": "value7", "field2": "value8"}
    ]';
    $temp = json_decode($json);
    $inserted = array( (object)[]);
    array_splice( $temp, 3, 0, $inserted );
    $result = json_encode($temp);
    var_dump($result);