1

I am returning a JSON object to Javascript using PHP and I want to loop through the object and then print each value in the code.

Here is the function:

function drawChart(data) {
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: [],
            datasets: [{
                label: 'FiveRP',
                data: [],
                backgroundColor: "rgba(75,192,192,0.4)",
                fill: false,
                lineTension: 0.1,
                borderColor: "rgba(75,192,192,1)",
                borderCapStyle: 'butt'
            }, {
                label: 'GTALife',
                data: [],
                backgroundColor: "rgba(148,0,211,0.4)",
                fill: false,
                lineTension: 0.1,
                borderColor: "rgba(148,0,211,1)",
                borderCapStyle: 'butt'
            }, {
                label: 'GermanV',
                data: [],
                backgroundColor: "rgba(255,165,0,0.4)",
                fill: false,
                lineTension: 0.1,
                borderColor: "rgba(255,165,0,1)",
                borderCapStyle: 'butt'
            }]
        }
    });
}

As you can see that labels: [] is empty, I need to extract the values from the JSON array and add them to '[]' .

I am returning the JSON through AJAX so I think I can not use PHP to iterate through the object.

Parker Queen
  • 619
  • 2
  • 12
  • 27
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Matt Burland May 24 '17 at 15:33
  • @MattBurland I know how to access stuff inside of the Object, the problem is something else. I want to add every value in JSON object inside the '[]' infont of 'labels' as provided in function. – Parker Queen May 24 '17 at 15:37
  • @MaxZoom What do you mean? – Parker Queen May 24 '17 at 15:38
  • @MaxZoom I am sorry for being a nitwit and not understanding. Can you please explain? The JSON is being returned from AJAX and is already a JS Object, what can PHP do in such regards? – Parker Queen May 24 '17 at 15:40
  • So datasets is your returned json? And you want to print out the vals in PHP? You don't need labels variable to do this. – ADoorMarkedPirate May 24 '17 at 15:41
  • It is unclear from the question which part of data is the JSON - since my confusion. I believe it is `datasets`. – MaxZoom May 24 '17 at 15:52

1 Answers1

1

Below code populates labels array with label values in JSON

var json = [{
                label: 'FiveRP',
                data: [],
                backgroundColor: "rgba(75,192,192,0.4)",
                fill: false,
                lineTension: 0.1,
                borderColor: "rgba(75,192,192,1)",
                borderCapStyle: 'butt'
            }, {
                label: 'GTALife',
                data: [],
                backgroundColor: "rgba(148,0,211,0.4)",
                fill: false,
                lineTension: 0.1,
                borderColor: "rgba(148,0,211,1)",
                borderCapStyle: 'butt'
            }, {
                label: 'GermanV',
                data: [],
                backgroundColor: "rgba(255,165,0,0.4)",
                fill: false,
                lineTension: 0.1,
                borderColor: "rgba(255,165,0,1)",
                borderCapStyle: 'butt'
            }];
            
var arr = [];
for (e of json) {
  arr.push(e.label)
}

var data = {
 labels: arr,
 datasets: json
}
console.log(data.labels);
            
MaxZoom
  • 7,619
  • 5
  • 28
  • 44