使用PHP更改Json数据格式以显示高图

时间:2014-04-10 06:59:24

标签: php arrays json multidimensional-array

我使用高图显示堆积条形图来显示数据,这需要采用以下格式的数据

预期产出

   [ {
                    name: 'data[0]',
                    data: [3, 4, 4, 2, 5, 7]
                },{
                    name: 'data[1]',
                    data: [2, 2, 3, 2, 1, 4]
                },{
                    name: 'data[2]',
                    data: [5, 3, 4, 7, 2, 6]
                }]

但由于某种原因,我只能通过AJAX调用获取此格式的数据

[
  {
    "name": 94,
    "data": [
      2,
      8,
      9
    ]
  },
  {
    "name": 93,
    "data": [
      "0",
      3,
      "0"
    ]
  },
  {
    "name": 92,
    "data": [
      "0",
      8,
      4
    ]
  },
  {
    "name": 91,
    "data": [
      3,
      18,
      8
    ]
  },
  {
    "name": 90,
    "data": [
      1,
      7,
      5
    ]
  },
  {
    "name": 89,
    "data": [
      2,
      9,
      3
    ]
  }
]

我需要单独数组中的所有名称,&数据[0],数据[1],数据[2]在单独的数组中,

我如何使用PHP实现这一点?

我正在尝试

foreach($jsonpost as $single) {
        foreach($single as $sing){
            echo $sing; echo "<br/>";
//arraypush(sing[0])

        }
    }

json_encode之前的数组格式

Array ( [0] => Array ( [name] => 94 [data] => Array ( [0] => 2 [1] => 8 [2] => 9 ) ) [1] => Array ( [name] => 93 [data] => Array ( [0] => 0 [1] => 3 [2] => 0 ) ) [2] => Array ( [name] => 92 [data] => Array ( [0] => 0 [1] => 8 [2] => 4 ) ) [3] => Array ( [name] => 91 [data] => Array ( [0] => 3 [1] => 18 [2] => 8 ) ) [4] => Array ( [name] => 90 [data] => Array ( [0] => 1 [1] => 7 [2] => 5 ) ) [5] => Array ( [name] => 89 [data] => Array ( [0] => 2 [1] => 9 [2] => 3 ) ) [6] => Array ( [name] => 88 [data] => Array ( [0] => 2 [1] => 1 [2] => 4 ) ) [7] => Array ( [name] => 87 [data] => Array ( [0] => 0 [1] => 0 [2] => 1 ) ) [8] => Array ( [name] => 86 [data] => Array ( [0] => 4 [1] => 8 [2] => 6 ) ) [9] => Array ( [name] => 85 [data] => Array ( [0] => 3 [1] => 6 [2] => 7 ) ) [10] => Array ( [name] => 84 [data] => Array ( [0] => 1 [1] => 9 [2] => 5 ) ) [11] => Array ( [name] => 83 [data] => Array ( [0] => 1 [1] => 3 [2] => 0 ) ) [12] => Array ( [name] => 82 [data] => Array ( [0] => 5 [1] => 3 [2] => 0 ) ) [13] => Array ( [name] => 81 [data] => Array ( [0] => 1 [1] => 4 [2] => 4 ) ) [14] => Array ( [name] => 80 [data] => Array ( [0] => 4 [1] => 8 [2] => 6 ) ) [15] => Array ( [name] => 79 [data] => Array ( [0] => 2 [1] => 7 [2] => 7 ) ) [16] => Array ( [name] => 78 [data] => Array ( [0] => 4 [1] => 7 [2] => 12 ) ) [17] => Array ( [name] => 77 [data] => Array ( [0] => 1 [1] => 4 [2] => 1 ) ) [18] => Array ( [name] => 76 [data] => Array ( [0] => 7 [1] => 11 [2] => 13 ) ) [19] => Array ( [name] => 75 [data] => Array ( [0] => 2 [1] => 9 [2] => 6 ) ) [20] => Array ( [name] => 74 [data] => Array ( [0] => 0 [1] => 5 [2] => 1 ) ) [21] => Array ( [name] => 73 [data] => Array ( [0] => 3 [1] => 5 [2] => 3 ) ) [22] => Array ( [name] => 72 [data] => Array ( [0] => 3 [1] => 16 [2] => 15 ) ) ) 

0 个答案:

没有答案