0

I'm trying to get some data from Wikipedia API. I found this project https://github.com/donwilson/PHP-Wikipedia-Syntax-Parser but I cannot figure out how to output the infobox entries in a loop, because the array is in an array which is in an array.

The array code

    [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.
                            )


                    )

            )

    )

This is what I tried (returns the value but not the key)

$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>";
}
Hamada Badran
  • 139
  • 2
  • 2
  • 8

3 Answers3

0

Let's just see what happens when we simplify everything a little bit:

$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>";
    }
}

The use of array_keys() and the for/foreach loops get a little confusing quickly, so I'm not sure exactly what your error is without looking more. The big trick with my code above is the use of reset() which resets an array and returns (the first element). This lets us grab the first infobox and check if it exists in the next line (before attempting to get the contents of a non-existent key). Next, we just loop through all contents of this first infobox and access the key and value keys directly.

Sam
  • 20,096
  • 2
  • 45
  • 71
0

You if want to access both keys and values, a simple good ol' foreach will do. Consider this example:

$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
}

Sample Output:

[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

user1978142
  • 7,946
  • 3
  • 17
  • 20
0

As an exercise in scanning the given array without hardcoding anything.

It doesn't simplify anything, it is not as clear as some of the other answers, however, it would process any structure like this whatever the keys were.

It uses the 'inherited' iterator, that all arrays have, for the 'types' and foreach for the contents.

<?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 />' ;
}
Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31