I'm trying to send an array to JS, but I can't get the array I want.
This is my Controller PHP code:
foreach ($test as $k => $subArray) {
foreach ($subArray as $id=>$value) {
array_key_exists( $id, $sumArray ) ? $sumArray[$id] += $value : $sumArray[$id] = $value;
}
}
array_push($t, $sumArray);
$data['res'] = array_values($t[0]);
array_push($temp, $data);
echo json_encode($temp);
The output was:
[{"res":[484603732,350203732,133347732,203347732]}]
This is my View code:
$.get('/index.php/dashboard/linechart', function(data){
console.log(data.res);
});
My expected output on console.log was:
[484603732,350203732,133347732,203347732]
but something goes wrong, the output went undefined.
So far I've tried with JSON.parse(data.res);
But still got undefined.
Anyone could save my day? I'm really stuck with this.
datato an object, your server sends a JSON string. Notice also, thatresis a member of the array, hencedata[0].resis what you need.let parsedData = JSON.parse(data); let res = parsedData[0].res;should do. Server always responses with a string, live objects are not going through the http(s).