使用 PHP 用动态线解码 json?
Decode json with dynamic line using PHP?
这是我在 json_decode:
之后的代码
stdClass Object (
[batchcomplete] =>
[query] => stdClass Object (
[pages] => stdClass Object (
[56667] => stdClass Object (
[pageid] => 56667
[ns] => 0
[title] => Hanoi
[contentmodel] => wikitext
[pagelanguage] => en
[touched] => 2015-10-25T20:13:21Z
[lastrevid] => 687471695
[length] => 53648
[fullurl] => https://en.wikipedia.org/wiki/Hanoi
[editurl] => https://en.wikipedia.org/w/index.php?title=Hanoi&action=edit
[canonicalurl] => https://en.wikipedia.org/wiki/Hanoi
)
)
)
)
如何使用 PHP 获取值 [title]
、[fullurl]
和 [pageid]
?我现在不知道如何通过 [56667] => stdClass Object (
行,因为 56667
是动态的(取决于请求)。
您可以使用reset()
获取第一个数组值。这不需要您知道密钥。
试试这个:
$output = json_decode($output, true); // convert to array so we can use reset.
$output_details = reset($output['query']['pages']);
$output_details['title']; // title
$output_details['fullurl']; // fullurl
$output_details['pageid']; // pageid
这是我在 json_decode:
之后的代码stdClass Object (
[batchcomplete] =>
[query] => stdClass Object (
[pages] => stdClass Object (
[56667] => stdClass Object (
[pageid] => 56667
[ns] => 0
[title] => Hanoi
[contentmodel] => wikitext
[pagelanguage] => en
[touched] => 2015-10-25T20:13:21Z
[lastrevid] => 687471695
[length] => 53648
[fullurl] => https://en.wikipedia.org/wiki/Hanoi
[editurl] => https://en.wikipedia.org/w/index.php?title=Hanoi&action=edit
[canonicalurl] => https://en.wikipedia.org/wiki/Hanoi
)
)
)
)
如何使用 PHP 获取值 [title]
、[fullurl]
和 [pageid]
?我现在不知道如何通过 [56667] => stdClass Object (
行,因为 56667
是动态的(取决于请求)。
您可以使用reset()
获取第一个数组值。这不需要您知道密钥。
试试这个:
$output = json_decode($output, true); // convert to array so we can use reset.
$output_details = reset($output['query']['pages']);
$output_details['title']; // title
$output_details['fullurl']; // fullurl
$output_details['pageid']; // pageid