PHP 数组为 html 输出从 API JSON 中提取或提取所有内容

PHP Array extract or pull everything from the API JSON for html output

我正在使用 PHP 并且我有这个 JSON 数据,我已经使用 file_get_content 和 json_decode 解码到我的 HTML 然后(

?>
    <pre>
       <?php
           print_r($animeList);
        ?>
    </pre>
 <?php

) 只是为了看看我在处理什么,但现在我需要将其格式化 nicely 到我的网页中,但它不起作用。它给了我一些日期并且不同集合之间没有空格。如何使用 foreach 循环从此数组输出数据?

这是我尝试过的方法(在 index.php 中)但是它给了我错误的输出:

if ($_GET['fields'] =='films'): 
            ?>
            <ol>
                <?php foreach ( $animeList as $films ) : ?>
                    <?php
                        foreach ($films as $url) : ?>
                        
                    <li>
                        <?php echo $url; ?>
                    </li>
                    <?php endforeach;  ?>
                <?php endforeach;  ?>
            </ol>
            <?php endif; ?> 
        <?php

这是我在 $animeList 中的内容(这是为了在网页中调试)

 Array
    (
        [0] => stdClass Object
            (
                [id] => 2baf70d1-42bb-4437-b551-e5fed5a87abe
                [title] => Castle in the Sky
                [description] => The orphan Sheeta inherited a mysterious crystal that links her to the mythical sky-kingdom of Laputa. With the help of resourceful Pazu and a rollicking band of sky pirates, she makes her way to the ruins of the once-great civilization. Sheeta and Pazu must outwit the evil Muska, who plans to use Laputa's science to make himself ruler of the world.
                [director] => Hayao Miyazaki
                [producer] => Isao Takahata
                [release_date] => 1986
                [rt_score] => 95
                [people] => Array
                    (
                        [0] => https://ghibliapi.herokuapp.com/people/
                    )
            )
    
    [1] => stdClass Object
        (
            [id] => 12cfb892-aac0-4c5b-94af-521852e46d6a
            [title] => Grave of the Fireflies
            [description] => In the latter part of World War II, a boy and his sister, orphaned when their mother is killed in the firebombing of Tokyo, are left to survive on their own in what remains of civilian life in Japan. The plot follows this boy and his sister as they do their best to survive in the Japanese countryside, battling hunger, prejudice, and pride in their own quiet, personal battle.
            [director] => Isao Takahata
            [producer] => Toru Hara
            [release_date] => 1988
            [rt_score] => 97
            [people] => Array
                (
                    [0] => https://ghibliapi.herokuapp.com/people/
                )
        )

    [2] => stdClass Object
        (
            [id] => 58611129-2dbc-4a81-a72f-77ddfc1b1b49
            [title] => My Neighbor Totoro
            [description] => Two sisters move to the country with their father in order to be closer to their hospitalized mother, and discover the surrounding trees are inhabited by Totoros, magical spirits of the forest. When the youngest runs away from home, the older sister seeks help from the spirits to find her.
            [director] => Hayao Miyazaki
            [producer] => Hayao Miyazaki
            [release_date] => 1988
            [rt_score] => 93
            [people] => Array
                (
                    [0] => https://ghibliapi.herokuapp.com/people/986faac6-67e3-4fb8-a9ee-bad077c2e7fe
                    [1] => https://ghibliapi.herokuapp.com/people/d5df3c04-f355-4038-833c-83bd3502b6b9
                    [2] => https://ghibliapi.herokuapp.com/people/3031caa8-eb1a-41c6-ab93-dd091b541e11
                    [3] => https://ghibliapi.herokuapp.com/people/87b68b97-3774-495b-bf80-495a5f3e672d
                    [4] => https://ghibliapi.herokuapp.com/people/d39deecb-2bd0-4770-8b45-485f26e1381f
                    [5] => https://ghibliapi.herokuapp.com/people/591524bc-04fe-4e60-8d61-2425e42ffb2a
                    [6] => https://ghibliapi.herokuapp.com/people/c491755a-407d-4d6e-b58a-240ec78b5061
                    [7] => https://ghibliapi.herokuapp.com/people/f467e18e-3694-409f-bdb3-be891ade1106
                    [8] => https://ghibliapi.herokuapp.com/people/08ffbce4-7f94-476a-95bc-76d3c3969c19
                    [9] => https://ghibliapi.herokuapp.com/people/0f8ef701-b4c7-4f15-bd15-368c7fe38d0a
                )
        )
)

所以我看到了这个输出,你可以看到 3 组不同的数据之间没有空格,并且 'people' 数组没有输出。

您可以使用嵌套列表来显示您的数据。

<ol>
    <?php foreach ( $animeList as $films ) : ?>
    <li>
        <ul>
            <?php foreach ($films as $url) : ?>
                <li><?php 
                    if (! is_array($url)) {
                        echo $url; 
                    }
                ?></li>
            <?php endforeach;  ?>
        </ul>
    </li>
    <?php endforeach;  ?>
</ol>

对于关键的“people”,你需要再做一个循环。

编辑 与“sub-list”:

<ol>
    <?php foreach ( $animeList as $films ) : ?>
    <li>
        <ul>
            <?php foreach ($films as $key => $url) : ?>
                <li>
                    <?php echo $key ?>

                    <?php 
                    if (! is_array($url)) {
                        echo $url; 
                    }
                    else {
                    ?>
                        <ul>
                            <?php foreach ($url as $value) : ?>
                                <li><?php echo $value ?></li>
                            <?php endforeach;  ?>
                        </ul>
                        <?php
                    }

                ?></li>
            <?php endforeach;  ?>
        </ul>
    </li>
    <?php endforeach;  ?>
</ol>

修正后的输出现在是这样的: