1

Here is my code:

var newval =[{"new_id":"1","new_no":"1","total":"N.A"},{"new_id":"2","new_no":"3","total":"4"},{"new_id":"2","new_no":"4","total":"5"}];

from this json value how to get details based on new_no value using javascript or php.

  function passfun(new_no)
  {
    alert(newval[0].plot_no);
  }

I tried like this but its working based on json array values i not getting exact values based on new_no

saravanan mp
  • 745
  • 3
  • 16
  • 34

1 Answers1

0

You have an array of objects in JSON format. In PHP you can use the following code:

PHP:

<?php
$json = '[{"new_id":"1","new_no":"1","total":"N.A"},{"new_id":"2","new_no":"3","total":"4"},{"new_id":"2","new_no":"4","total":"5"}]';

$data = json_decode($json);

function get_prop($object, $name)
{
    return $object->{$name};

}


print get_prop($data[0], 'new_no')."\n";
print get_prop($data[0], 'new_id')."\n";

JavaScript:

var newval =[{"new_id":"1","new_no":"1","total":"N.A"},
             {"new_id":"2","new_no":"3","total":"4"},
             {"new_id":"2","new_no":"4","total":"5"}];

function get_prop(obj, name)
{
    return obj[name];
}

alert(get_prop(newval[0], 'new_no'));
user4035
  • 22,508
  • 11
  • 59
  • 94