Here is my array that is put together:
Array
(
[1] => Array
(
[0] => Array
(
[date] => 1335848400000
[value] => 1
[product_id] => 1
[title] => Test Product
)
[1] => Array
(
[date] => 1338526800000
[value] => 8
[product_id] => 1
[title] => Test Product
)
)
[2] => Array
(
[0] => Array
(
[date] => 1335848400000
[value] => 1
[product_id] => 2
[title] => Test Product 2
)
[1] => Array
(
[date] => 1338526800000
[value] => 4
[product_id] => 2
[title] => Test Product 2
)
)
[3] => Array
(
[0] => Array
(
[date] => 1338526800000
[value] => 6
[product_id] => 3
[title] => Test Product 3
)
)
)
What I would like it to display as:
{
name: 'Test Product',
data: [[1335848400000, 1],[1338526800000, 8]]
},
{
name: 'Test Product 2',
data: [[1335848400000, 1], [1338526800000, 4]]
},
{
name: 'Test Product 3',
data: [[1338526800000, 6]]
},
I need to group by dates and values together as such for highcharts.
How would I go about doing that? I have no idea where to even start. I have done research into it but couldn’t find a good example.
EDIT:
Working code except with one error:
for($i=0 ; $i<=count($array) ; $i++)
{
$result_array[$i]['name'] = $array[$i][0]['title'];
for($j=0 ; $j<count($array[$i]) ; $j++)
{
$result_array[$i]['data'][$j][0] = $array[$i][$j]['date'];
$result_array[$i]['data'][$j][1] = $array[$i][$j]['value'];
}
}
echo $result = json_encode($result_array);
error:
Severity: Notice
Message: Undefined offset: 0
Filename: name.php
Line Number: 115
[{"name":null},{"name":"Test Product","data":[[1335848400000,"1"],[1338526800000,"8"]]},{"name":"Test Product 2","data":[[1335848400000,"1"],[1338526800000,"4"]]},{"name":"Test Product 3","data":[[1338526800000,"6"]]}]
You can use this loop
I have tested it… It works compaletely as you want