php - 将对象插入 JSON 对象
问题描述
我正在尝试将 json 对象添加到从我的数据库下载的对象中,但是当我添加对象时,它并没有真正位于对象中的同一位置,它会将每个附加对象添加为第一个对象的子对象。请看下文。
Array
(
[0] => stdClass Object
(
[component] => fork
[date_] => 06/08/2019
[servicetype] => rtyrtyrty
)
[1] => Array
(
[0] => stdClass Object
(
[component] => fork
[date_] => 20/05/2019
[servicetype] => rtyrtyrty
)
)
[2] => Array
(
[0] => stdClass Object
(
[component] => fork
[date_] => 10/02/2019
[servicetype] => rtyrtyrty
)
)
[3] => Array
(
[0] => stdClass Object
(
[component] => fork
[date_] => 11/12/2018
[servicetype] => rtyrtyrty
)
)
)
我一直在尝试使用 json_decode() 将原始对象转换为数组,然后使用 push 函数:array_push($array1, $array2); 我似乎无法避免得到上述结果。
originalObj= json_decode($newObj);
array_push($originalObj, $newObj);
$encodeForServerUpload = json_encode($originalObj);
解决方案
改用这个:
$originalObj= json_decode($newObj);
array_push($originalObj, $newObj[0]);
$encodeForServerUpload = json_encode($originalObj);
推荐阅读
- vim - 在 Vim 中跨多行选择句子中间的视觉块
- json - 如何将具有嵌入式结构字段的结构编组为 Go 中的平面 JSON 对象?
- linux - NASM Linux 共享对象错误:针对“.data”重定位 R_X86_64_32S
- web-scraping - 如何为输入的自定义查询获取 Google 新闻链接
- ios - SwiftUI:onDismiss 不会被调用
- node.js - 在本机反应中使用firebase显示来自留言板的消息
- python - 两个现有的 Numpy 数组,有条件地填充 NaN
- python - 如何使用循环来获取每个元素?
- bash - Bash 以冒号和撇号开头。意义
- angular - 获取 TypeError:无法读取未定义的属性“queryService”