您好我正试图通过此Link
获取此json文件中的fulltext
值
我只获得数组顶部如何从所有数组中获取所有值,甚至是嵌套数组
$json_output = json_decode($json, true);
var_dump($json_output);
foreach($json_output['results'] as $item) {
echo '<br/>'. $item['fulltext'];
}
答案 0 :(得分:0)
我尝试在PHP中查看spl,它为您提供了有用的迭代器类,包括专门用于复杂数组的类 - http://php.net/manual/en/class.recursivearrayiterator.php
答案 1 :(得分:0)
另一种方法是
1)将你的json转换为xml(json - &gt; php array - &gt; xml) - 你可以使用this递归函数:
function to_xml(SimpleXMLElement $object, array $data)
{
foreach ($data as $key => $value)
{
if (is_array($value))
{
$new_object = $object->addChild($key);
to_xml($new_object, $value);
}
else
{
$object->addChild($key, $value);
}
}
}
$xml = new SimpleXMLElement('<rootTag/>');
to_xml($xml, $my_array);
2)使用XPath查询xml以获取所需的数据集合,例如
$titles = $xml->xpath('//@fulltext');
利用XPath,您可以通过复杂的约束轻松获取数据,请查看the docs
答案 2 :(得分:0)
找到解决方案
foreach($json_output['results'] AS $result) {
foreach($result['printouts']['Covers topic'] AS $topic) {
echo "<ul><li>".$topic['fulltext']."</li></ul>";
}
echo "<br/>".$result['fulltext'];
}
感谢您的帮助