0

I need to create a multi-dimensional array in php and want to use it in a jQuery script as a JSON array of objects;

The required output in the jQuery script should look like this:

data = [
        { Month:'April', Comms:1000, Fees:200, Gains: 200},
        { Month:'May',   Comms:1200, Fees:300, Gains: 300}
        ]   

Currently my php arrays are generated as follow:

    $data1[] = array(
        'Month' => 'April',
        'Comms' => 1000,
        'Fees'  => 200,
        'Gains' => 200      
    );      
    $data2[] = array(
        'Month' => 'May',
        'Comms' => 1200,
        'Fees'  => 300,
        'Gains' => 300      
    );

    echo json_encode($data);

My question is how to combine data1 and data2 into the data array in the json_encode php function which will produce the required jQuery JSON array of objects?

I do have the values of the different array fields and can create data1 and data2 in a different way, so the data is flexible and I can combine them in any other way which will produce the data array which will output them in the required JSON format.

Any help will be highly appreciated, I have seen question regarding this subject but none which address the issue I am facing.

6 Answers 6

1

You'll want to merge both Arrays into a new Array of Arrays. See the manual for more information.

$data = array_merge($data1, $data2);
echo json_encode($data)

or, more simply by using the + operator:

echo json_encode($data1 + $data2)
2
  • Thanks, as a matter of fact, I did tried $data1 + $data2 but I kept on getting only the first row. My situation is slightly more complicated than in my original question. I created these arrays in the model in a Joomla application, which return the combined array to the controller, where I use the json_encode. I tried to use the json_encode in the model and return that result to the controller but with the + operator only the first row was displayed but the array_merge did the trick. Commented Jul 19, 2012 at 10:41
  • Most likely a limitation of Joomla. I'm glad you got it working. Commented Jul 19, 2012 at 10:44
1

Try :

echo json_encode(array_merge($data1, $data2));
0
1

Simply:

echo json_encode($data1 + $data2);

Note that you can also use + to merge arrays.

1
  • Thanks, this should work but for one reason or the other I could only get the first row. Could be because of the use on the model and controller in Joomla, but I will keep this in mind. Commented Jul 19, 2012 at 10:45
1

Write:

echo json_encode($data1 + $data2);
0

just user

echo json_encode($data1 + $data2);
0

You should make another array by extracting data from database by using groupBy months and store the data in other array.

$array = array();  
foreach ($data as $element) {
    $array[$element['month']][] = [ 'comms' => $element['comms'], 'fees' => $element['fees'], 'gains' => $element['gains']
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.