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

2
  • That's not JSON. It's just an array with objects in it. Commented Dec 9, 2014 at 10:55
  • ok. how to get values from that using particular key pair @Quentin Commented Dec 9, 2014 at 10:59

1 Answer 1

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'));
5
  • your not getting my point. If we pass one parameter(example: new_no) in a method using that i need to get other values also in that array. Commented Dec 9, 2014 at 11:04
  • alert(newval[0].new_no); this method i tried already. Commented Dec 9, 2014 at 11:12
  • @SaravananMP Ahh, I see. I'll try and let you know. Commented Dec 9, 2014 at 11:14
  • same thing repeated. Commented Dec 9, 2014 at 11:40
  • @SaravananMP Sorry, I don't understand, what you mean. The PHP code is getting property value by text name. The same for javascript. Commented Dec 9, 2014 at 12:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.