0

I have this JSON array in a separate file on my server:

data = '[{"name" : "AAA", "year" : "1999", "plot" : "BBB", "run" : "194 min", "rated" : "PG-13", "score" : "7.7/10", "source" : "DDD", "id" : "000000"}]';

how would you with PHP get the "name" (AAA) in the array?

Sorry if im missing anything, just starting out with PHP and JSON.

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
S. Doe
  • 67
  • 6

3 Answers3

0

In PHP, you can get name from json response as:

// your json response
$string = '[{"name" : "AAA", "year" : "1999", "plot" : "BBB", "run" : "194 min", "rated" : "PG-13", "score" : "7.7/10", "source" : "DDD", "id" : "000000"}]';

$encoded = json_decode($string,TRUE);
print_r($encoded); // complete array
echo $encoded[0]['name']; // print AAA
devpro
  • 16,184
  • 3
  • 27
  • 38
0
$data = '[{"name" : "AAA", "year" : "1999", "plot" : "BBB", "run" : "194 min", "rated" : "PG-13", "score" : "7.7/10", "source" : "DDD", "id" : "000000"}]';

$data_array = json_decode($data);
var_dump(current($data_array)->name);

Is this what you wanted ?

Dexy86
  • 72
  • 1
  • 5
0

If you have some data in JSON format and you want to refer any one of them . You can use following methods depending on your programming language :-

PHP :-

$data =  '[{"name" : "AAA", "year" : "1999", "plot" : "BBB", "run" : "194 min", "rated" : "PG-13", "score" : "7.7/10", "source" : "DDD", "id" : "000000"}]';
$parsedData = json_decode($data,TRUE);

Now $parsedData is an array and you can access values using key like as

foreach($parsedData as $key => $value)
{
     $name = $value['name'];
}

JavaScript :-

data =  '[{"name" : "AAA", "year" : "1999", "plot" : "BBB", "run" : "194 min", "rated" : "PG-13", "score" : "7.7/10", "source" : "DDD", "id" : "000000"}]';
var parsedData = JSON.parse(data);
var dataCount = parseData.length,index;
for(index = 0 ; index < dataCount ; i++)
{
     var name = parsedData[index]['name']; 
     // var name = parsedData[index].name;  // or 
}
Deepak Dixit
  • 1,510
  • 15
  • 24