Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I use highchart to display stacked bar chart to display data, which require data in the following format

Expected Output

   [ {
                    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]
                }]

But for some reason i could get the data only in this format through AJAX call

[
  {
    "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
    ]
  }
]

I need all name in seperate array, & data[0], data[1], data[2] in seperate arrays,

How can i implement this using PHP?

I am trying with

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

        }
    }

The array format before 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 ) ) ) 
share|improve this question
    
So, Whats your expected output? –  Dikesh 21 hours ago
    
[ { 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] }] –  Jagan K 20 hours ago
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.