0

I'm trying to merge two different json arrays into one object. The json arrays have different data (in terms of the data itself and the structure):

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

What I would like to get is something like this:

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

Then, in javascript I would like te be able to refer to the different json arrays like this:

dataprovider: datafortableandchart.datafortable

Is this possible?

dexter
  • 155
  • 2
  • 15

1 Answers1

0

First convert them to array then use array_merge to merge to arrays and again json_encode them

json_encode(array_merge(json_decode($datafortable , true),json_decode($dataforchart , true)))
urfusion
  • 5,528
  • 5
  • 50
  • 87