0

I am try to format an array to a JSON object that highcharts supports. My array from the database is as follows:

Array
(
    [0] => Array
        (
            [Group_ID] => 1
            [Name] => A line graph
            [month] => 4
            [amount] => 7700
        )

    [1] => Array
        (
            [Group_ID] => 2
            [Name] => B Line graph
            [month] => 4
            [amount] => 390
        )

    [2] => Array
        (
            [Group_ID] => 1 
            [Name] => A line graph
            [month] => 5
            [amount] => 5000
        )

    [3] => Array
        (
            [Group_ID] => 2
            [Name] => B line graph
            [month] => 5
            [amount] => 210
        )
)

I need to create an array like this to be able to create a highchart compatible JSON object:

Array
(
    [0] => Array
        (
            [name] => A revenue
            [data] => Array
                (
                    [4] => 7700 //amount for the fourth month
                    [5] => 5000 //amount for the fifth month
                )
        )

    [1] => Array
        (
            [name] => B revenue
            [data] => Array
                (
                    [4] => 390 //amount for the fourth month
                    [5] => 210 //amount for the fifth month
                )
        )
)

I have managed to come up with this array using my foreach but I cant seem to find a way to do it correctly:

Array
(
    [0] => Array
        (
            [name] => A line graph
            [amount] => 7700
            [month] => 4
        )

    [1] => Array
        (
            [name] => B line graph
            [amount] => 390
            [month] => 4
        )

    [2] => Array
        (
            [name] => A line graph
            [amount] => 5000
            [month] => 5
        )

    [3] => Array
        (
            [name] => B line graph
            [amount] => 210
            [month] => 5
        )

)

My foreach:

 foreach ($data as $key => $value) {
        $r[] = [
            'name' => $value['Line_GraphName'],
            'data' => $value['amount'],
            'month' => $value['month']
        ];
 }
2
  • 1
    AbraCadaver has the right answer, I would just point out for future reference in your foreach loops, don't get the $key unless you're going to use it. You're just wasting resources at that point. Small thing, for sure. Commented May 26, 2016 at 18:58
  • Thank you for that Doyle Lewis, had no idea on that :0 Commented May 26, 2016 at 21:04

2 Answers 2

3

You can create it very simply like this:

foreach ($data as $value) {
    $r[$value['Group_ID']]['name'] = $value['Line_GraphName']; 
    $r[$value['Group_ID']]['data'][$value['month']] = $value['amount'];
}
  • Loop and create a result with Group_ID as the key and add the name key and value
  • Add data array and append month as key and amount as value

If you don't like having Group_ID as the key and want to re-index:

$r = array_values($r);
Sign up to request clarification or add additional context in comments.

Comments

0

The solution using array_walk and array_values functions:

$result = [];
array_walk($data, function($v) use(&$result) {
    $key = $v['Group_ID'] . $v['Name'][0];  // compound key: "group_id + revenue's prefix"
    if (!isset($result[$key])) {
        $result[$key] = ['name' => $key[1] . " Revenue", 'data' => [$v['month'] => $v['amount']]];
    } else {
        $result[$key]['data'][$v['month']] = $v['amount'];
    }
});

print_r(array_values($result));

The output:

Array
(
    [0] => Array
        (
            [name] => A Revenue
            [data] => Array
                (
                    [4] => 7700
                    [5] => 5000
                )    
        )    
    [1] => Array
        (
            [name] => B Revenue
            [data] => Array
                (
                    [4] => 390
                    [5] => 210
                )
        )
)

Comments

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.