Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从JSON数组获取信息_Php_Arrays_Json - Fatal编程技术网

PHP从JSON数组获取信息

PHP从JSON数组获取信息,php,arrays,json,Php,Arrays,Json,我的服务器上有一个单独的文件中有这个JSON数组: data = '[{"name" : "AAA", "year" : "1999", "plot" : "BBB", "run" : "194 min", "rated" : "PG-13", "score" : "7.7/10", "source" : "DDD", "id" : "000000"}]'; 使用PHP如何获得数组中的“名称”(AAA) 抱歉,如果我遗漏了什么,就从PHP和JSON开始吧 在PHP中,您可以从json响应中获得

我的服务器上有一个单独的文件中有这个JSON数组:

data = '[{"name" : "AAA", "year" : "1999", "plot" : "BBB", "run" : "194 min", "rated" : "PG-13", "score" : "7.7/10", "source" : "DDD", "id" : "000000"}]';
使用PHP如何获得数组中的“名称”(AAA)


抱歉,如果我遗漏了什么,就从PHP和JSON开始吧

在PHP中,您可以从json响应中获得
name
,如下所示:

// 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

这就是您想要的吗?

如果您有一些JSON格式的数据,并且希望引用其中任何一个。根据您的编程语言,您可以使用以下方法:-

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);
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 
}
现在,$parsedData是一个数组,您可以使用如下键访问值

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"}]';
$parsedData = json_decode($data,TRUE);
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 
}
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(数据);
var dataCount=parseData.length,index;
对于(索引=0;索引
您的代码丢失。添加我们将帮助您的代码您是否尝试从文件中获取此值?请分享您的代码?可能重复:=-谢谢!正是我需要的@美国能源部:很高兴帮助你