簡體   English   中英

試圖在php中抓取json的數據不起作用

[英]trying to scrape data of json in php not working

為什么這行不通,它應該刮掉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 );
?>

$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'];

您的api輸出一些數組,列表是其中之一,用於訪問列表的內容,請執行以下操作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM