mukhlis
mukhlis

Reputation: 87

parsing Javascript array or object with php

I have a page that responds to javascript like this and I want to parse the 'series' array with php

$(function () {
   $('#container').highcharts({

      series: [{
          name: 'IP',
          data: [3.09,3.24,3.33,]

      }, {
          name: 'IPK',
          data: [3.09,3.16,3.22,]

      }]
  });
});

what is the most effective method for doing that ?

Upvotes: 0

Views: 70

Answers (1)

Xyz
Xyz

Reputation: 6013

In php you have json_decode to parse JSON data

var_dump(json_decode($series, true));

E.g.

  var output = {series: [{
      name: 'IP',
      data: [3.09,3.24,3.33,]

  }, {
      name: 'IPK',
      data: [3.09,3.16,3.22,]

  }];
  // Send output of JSON.stringify(output) to php via POST, GET or XHR.

And in php:

$input = json_decode($_GET['input'], true);
// $input = array('output' => 
               array('series' =>
                   array(
                       'name' => 'IP',
                       'data' => array(3.09, 3.24, 3.33)
                   ),
                   array(
                       'name' => 'IPK',
                       'data' => array(3.09, 3.16, 3.22)
                   )
               )
            );

Upvotes: 1

Related Questions