0

这是我放在一起的数组:

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
            )

    )

)

我希望它显示为:

{
name: 'Test Product',
data: [[1335848400000, 1],[1338526800000, 8]]
},
{
name: 'Test Product 2',
data: [[1335848400000, 1], [1338526800000, 4]]
},
{
name: 'Test Product 3',
data: [[1338526800000, 6]]
},

对于highcharts,我需要按日期和值分组。

我该怎么做呢?我什至不知道从哪里开始。我已经对其进行了研究,但找不到一个很好的例子。

编辑:

工作代码,除了一个错误:

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);

错误:

严重性:通知

消息:未定义的偏移量:0

文件名:name.php

行号: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"]]}] 
4

1 回答 1

1

你可以使用这个循环

<?php

for($i=1 ; $i<=count($main_array) ; $i++)
{
   $result_array[$i]['name'] = $main_array[$i][0]['title'];
   for($j=1 ; $j<=count($main_array[$i]) ; $j++)
   {
       $result_array[$i]['data'][$j][0] = $main_array[$i][$j]['date'];
       $result_array[$i]['data'][$j][1] = $main_array[$i][$j]['value'];
   }
}


echo $result = json_encode($result_array);

?>

我已经对其进行了测试...它可以根据您的需要完全工作

于 2012-06-15T16:47:51.427 回答