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.

6
  • Possible duplicate of Access / process (nested) objects, arrays or JSON Commented May 24, 2017 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. Commented May 24, 2017 at 15:37
  • @MaxZoom What do you mean? Commented May 24, 2017 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? Commented May 24, 2017 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. Commented May 24, 2017 at 15:41

1 Answer 1

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);
            

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.