【问题标题】:php merge two JSON arrays into one object [duplicate]php将两个JSON数组合并为一个对象[重复]
【发布时间】:2018-05-24 17:53:55
【问题描述】:

我正在尝试将两个不同的 json 数组合并到一个对象中。 json 数组有不同的数据(就数据本身和结构而言):

datafortable = [{"name": 3,"amount": "1190042293","category": "cars"}]
dataforchart = [{"name": 3,"amount": "5801"}]

我想得到的是这样的:

datafortableandchart = {
    "datafortable": [
        {
            "name": 3,
            "amount": "1190042293",
            "category": "cars"
        }
    ],
    "dataforchart": [
        {
            "name": 3,
            "amount": "5801"
        }
    ]
}

然后,在javascript 中,我希望您能够像这样引用不同的json 数组:

dataprovider: datafortableandchart.datafortable

这可能吗?

【问题讨论】:

  • datafortable .concat(dataforchart);
  • @dexter 是用js还是php合并数组?
  • @splash58 我想用 php 完成这项工作
  • @dexter echo json_encode(array_map('json_decode', compact('datafortable', 'dataforchart')), JSON_PRETTY_PRINT);
  • @dexter eval.in/916737

标签: javascript php arrays json


【解决方案1】:

首先将它们转换为数组,然后使用array_merge 合并到数组,然后再次使用json_encode 它们

json_encode(array_merge(json_decode($datafortable , true),json_decode($dataforchart , true)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 2022-01-22
    • 2011-10-18
    • 2021-11-12
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多