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 created an array in php (as part of an ajax response) . I would now like to convert this array into a javascript array of objects. How can I do this please?

My php array (please not this is wordpress php):

$fortot2 = 5;
$fortot3 = 2;

if (is_numeric($numwelds) && is_numeric($numconwelds))
{
    $total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
    $total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
    $total['tot3'] = ($numwelds + $numconwelds) + $fortot2 / $fortot3; 
    $response = json_encode($total);
    header("Content-Type: application/json");  
    echo $response;

Now, how can I convert this into a javascript array of objects, once the json has been encoded?

share|improve this question
    
Are you getting this with ajax as there's a header and everything ? –  adeneo 1 hour ago
    
yes, this is the response for my ajax. The variables are added up to create the totals in the ajax. Now I want to take those 3 totals in the array I put them in, & convert them to a js array of objects. –  mattnewbie 55 mins ago
add comment

1 Answer

// responseData is fetched via Ajax-Get
var obj = jQuery.parseJSON(responseData);
alert(obj.property);

// full ajax example
jQuery.ajax({
    url: "/index.php?action=whereMyDataIs", // here php gives the json response
    type: "GET",
    dataType: "json",
    async: false,
    success: function (data) {
        console.log(data);
        alert(JSON.stringify(data));
    }
});

You already have the PHP part (array is build, json_encoded and send as response), the next step is that you fetch this json_response on client-side, by doing a ajax get request to the PHP script providing the response. By specifying dataType:"json" you tell jQuery to automatically parse the incoming data as Object Notation. If you want to output it again, e.g. with alert, you need to stringify it again.

To answer the question regarding amcharts:

// add the loadJson function to AmCharts
// but you could also use jQuery as demonstrated above
AmCharts.loadJSON = function(url) {
  // create the request
  if (window.XMLHttpRequest) {
    // IE7+, Firefox, Chrome, Opera, Safari
    var request = new XMLHttpRequest();
  } else {
    // code for IE6, IE5
    var request = new ActiveXObject('Microsoft.XMLHTTP');
  }

  // load it
  // the last "false" parameter ensures that our code will wait before the
  // data is loaded
  request.open('GET', url, false);
  request.send();

  // parse adn return the output
  return eval(request.responseText);
};

// init chart library
var chart;

// lets build that chart
AmCharts.ready(function() {

   // load the data from your php script (the json response)
   var chartData = AmCharts.loadJSON('script.php');

   // verify in browser console, if the data is loaded and parsed correctly
   console.log(chartData);

   // build the chart
   // ...

   // output the chart
   chart.write("chartdiv");
   });
share|improve this answer
    
understand you! You need to use JSON - Yeah, you make it already with $response = json_encode($total); and header("Content-Type: application/json");. You write, that you need it for an ajax request. I think you understand the JSON result. You can convert it in javascript with JSON.parse(). But what its your directly problem? The jQuery example is an example. Learn the basics to realize your project. –  Adrian Preuss 43 mins ago
    
Please be nice to each other. We all started as beginners. –  Jens-André Koch 39 mins ago
    
@Jens-AndréKoch Thanks. Sorry for the basic questions. So can I use jQuery.parseJSON to convert it into a JavaScript array of objects like this amcharts.com/tutorials/your-first-chart-with-amcharts ? Thanks –  mattnewbie 30 mins ago
    
Wait a minute, i need to look at the amcharts library, to see, if one could feed it directly with json data. –  Jens-André Koch 21 mins ago
    
Yes, it's possible to feed Amcharts directly with the JSON response from your PHP script. I have updated my answer accordingly. –  Jens-André Koch 17 mins ago
show 2 more comments

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.