PHP Array从API JSON中提取或提取所有内容以进行html输出

问题描述

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

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

)只是为了查看我要处理的内容,但现在我需要很好地将其设置为我的网页格式,但是它不起作用。它给了我一些日期,不同集合之间没有空格。现在如何使用foreach循环从该数组输出数据?

这是我尝试过的操作( inside 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'数组。

enter image description here

解决方法

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

Forbidden (CSRF cookie not set.): /blog/create/


# views.py
class CreatePost(CreateAPIView):
    permission_classes = (permissions.AllowAny,)
    queryset = BlogPost.objects.all()
    serializer_class = BlogCreateSerializer

# urls.py
path('create/',CreatePost.as_view()),

对于键“ people”,您需要执行另一个循环。

EDIT 和“子列表”:

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

这是校正后的输出现在的样子: enter image description here