Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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,为什么这不起作用呢?它应该与pp的定义擦肩而过,但它不起作用:/ <?php $json_output = file_get_contents("http://api.urbandictionary.com/v0/define?term=pp"); $json = json_decode($json_output, true); $chuck_noris = $json['list']['definition']; print_r($chuck_noris ); ?> 您的api输

为什么这不起作用呢?它应该与pp的定义擦肩而过,但它不起作用:/

<?php
$json_output = file_get_contents("http://api.urbandictionary.com/v0/define?term=pp");
$json = json_decode($json_output, true);
$chuck_noris = $json['list']['definition'];
print_r($chuck_noris );
?>

您的api输出几个数组,list就是其中之一,可以访问list的内容,请执行以下操作

foreach($json->list as $something)
{
$chuck_noris = $something->definition;
echo $chuck_noris;
}

$json['list']
中仍然有维度(它仍然是一个数组)。您可以使用foreach获取其中的值:

$json_output = file_get_contents("http://api.urbandictionary.com/v0/define?term=pp");
$json = json_decode($json_output, true);
foreach($json['list'] as $list) {
    // $list will hold each array inside `$json['list']`
    echo $list['definition'] . '<br/>';
}
echo $json['list'][0]['definition'];