Dinesh Gupta Dinesh Gupta - 3 months ago 12
PHP Question

How to access inside the multi-dimensional array?

I need to access the id inside [poll] and also the [id] inside [options] using console.log. I tried various ways but I am unable to. How do I access these? I tried "Cannot read property 'id' of undefined" when I tried

console.log(getPollById(response[poll].id));
Can you please tell me how to access these?

Array
(
[poll] => stdClass Object
(
[id] => 4
[story_id] => 27280
[user_id] => 6636
[question] => So, what's your answer?
[status] => 1
[doc] => 2016-08-24 12:41:12
)

[options] => Array
(
[0] => stdClass Object
(
[id] => 13
[options] => 29
[image_url] => agseg7jgvnnel020r626.jpeg
[doc] => 2016-08-24 12:41:13
[votes] => 6
)
)
)

Answer

try it this way

Var jsondata = <?php echo json_encode($arr, false); ?>;


 Var dataArr=JSON.parse(jsondata);

//now you can access your JavaScript array

or

in php script

$jsondata = json_encode($arr);

in javascript

var jsondata = <?php echo $jsondata; ?>;
var dataArr = JSON.parse(jsondata); //this is your array data readable in javascript

This code is not tested though

Comments