从嵌套数组返回数据

时间:2014-05-27 18:12:11

标签: php arrays wikipedia-api

我试图从维基百科API获取一些数据。我找到了这个项目https://github.com/donwilson/PHP-Wikipedia-Syntax-Parser,但我无法弄清楚如何在循环中输出信息框条目,因为array位于array中的array。< / p>

数组代码

    [infoboxes] => Array
    (
        [0] => Array
            (
                [type] =>  musical artist 
                [type_key] => musical_artist
                [contents] => Array
                    (
                        [0] => Array
                            (
                                [key] => name
                                [value] => George Harrison <br /><small>[[Order of the British Empire|MBE]]</small>
                            )

                        [1] => Array
                            (
                                [key] => image
                                [value] => George Harrison 1974 edited.jpg
                            )

                        [2] => Array
                            (
                                [key] => alt
                                [value] => Black-and-white shot of a moustachioed man in his early thirties with long, dark hair.
                            )

                        [3] => Array
                            (
                                [key] => caption
                                [value] => George Harrison at the White House in 1974.
                            )


                    )

            )

    )

这是我尝试的(返回值但不是键)

$values=$parsed_wiki_syntax["infoboxes"][0]["contents"];
$keys = array_keys($values);

for($i=0; $i<count($values); $i++){
    foreach ($values[$keys[$i]] as $key=>$value)
        echo "<b>".$key."</b>: ".$value."<br><br>";
}

3 个答案:

答案 0 :(得分:0)

让我们看看当我们简化一切时会发生什么:

$firstBox = reset($parsed_wiki_syntax['infoboxes']);
if($firstBox) {
    foreach($firstBox['contents'] as $content) {
        $key = $content['key'];
        $value = $content['value'];
        echo "<b>" . $key . "</b>: " . $value . "<br><br>";
    }
}

array_keys()for / foreach循环的使用很快就会引起一些混乱,所以我不确定你的错误究竟是什么而不看更多。我上面的代码的一个重要技巧是使用reset()重置数组并返回(第一个元素)。这使我们可以抓住第一个infobox并检查它是否存在于下一行中(在尝试获取不存在的密钥的contents之前)。接下来,我们只需遍历此contents的所有infobox,然后直接访问keyvalue个键。

答案 1 :(得分:0)

如果你想要访问键和值,你可以做一个简单的好事。考虑这个例子:

$values = array('infoboxes' => array(array('type' => 'musical artist','type_key' => 'musical_artist','contents' => array(array('key' => 'name', 'value' => 'George Harrison <br /><small>[[Order of the British Empire|MBE]]</small>'),array('key' => 'image', 'value' => 'George Harrison 1974 edited.jpg'),array('key' => 'alt', 'value' => 'Black-and-white shot of a moustachioed man in his early thirties with long, dark hair.'),array('key' => 'caption', 'value' => 'George Harrison at the White House in 1974.'),),),),);
$contents = $values['infoboxes'][0]['contents'];
foreach($contents as $key => $value) {
    echo "[key => " . $value['key'] . "][value = " . htmlentities($value['value']) . "]<br/>";
    // just used htmlentities just as to not mess up the echo since there are html tags inside the value
}

示例输出:

[key => name][value = George Harrison <br /><small>[[Order of the British Empire|MBE]]</small>]
[key => image][value = George Harrison 1974 edited.jpg]
[key => alt][value = Black-and-white shot of a moustachioed man in his early thirties with long, dark hair.]
[key => caption][value = George Harrison at the White House in 1974.]

Sample Fiddle

答案 2 :(得分:0)

作为扫描给定数组而不用硬编码的练习。

它没有简化任何事情,它不像其他一些答案那样清晰,但是,无论密钥是什么,它都会像这样处理任何结构。

它使用&#39;继承&#39;迭代器,所有数组都有,对于&#39;类型&#39;并预告内容。

<?php
$values = array('infoboxes' => array(array('type' => 'musical artist','type_key' => 'musical_artist','contents' => array(array('key' => 'name', 'value' => 'George Harrison <br /><small>[[Order of the British Empire|MBE]]</small>'),array('key' => 'image', 'value' => 'George Harrison 1974 edited.jpg'),array('key' => 'alt', 'value' => 'Black-and-white shot of a moustachioed man in his early thirties with long, dark hair.'),array('key' => 'caption', 'value' => 'George Harrison at the White House in 1974.'),),),),);

$typeList = current(current($values));
echo key($typeList), ' => ', current($typeList), '<br />';
next($typeList);

echo key($typeList), ' => ', current($typeList), '<br />';
next($typeList);

echo key($typeList), '...', '<br />';
foreach(current($typeList) as $content) {
    $key = current($content);
    next($content);
    echo 'content: ', $key, ' => ', current($content), '<br />' ;
}