user3253289
user3253289

Reputation: 63

Getting this json data and displaying it

I have this JSON in my php, how can I loop through it using jQuery and display the contents?

$data = array(
(object)array(
    'oV' => 'myfirstvalue',
    'oT' => 'myfirsttext',
),
(object)array(
    'oV' => 'mysecondvalue',
    'oT' => 'mysecondtext',
),
);

NEW CODE VARIATION:

for($d=0;$d<4;$d++){
  $data = array(
(object)array(
    'oV' => myfirstvalue[d],
    'oT' => myfirsttext[d],
),
);
}

Upvotes: 0

Views: 49

Answers (1)

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

json encoding your array

echo json_encode($data);

would produce

[
  {
     "oV":"myfirstvalue",
     "oT":"myfirsttext"
  },
  {
     "oV":"mysecondvalue",
     "oT":"mysecondtext"
  }
]

which should be the json response you want to echo back as the ajax response.

so to loop over this json response in javascript, do like

for(var i=0;i<data.length;i++) {

    console.log(data[i].oV,data[i].oT); 

}

Upvotes: 1

Related Questions