PHP如何回显JSON对象

问题描述 投票:0回答:1

我曾尝试用explode()函数回显JSON对象,但没有成功。我认为我使用了错误的方法,但是找不到其他方法。

我想回显类似的JSON:

{
    "Title_Book": "Harry Potter",
    "Volume": [
        {
            "Name": "Harry Potter and the Philosopher's Stone",
            "Actor": [
                {
                    "Actor": "Actor0"
                },
                {
                    "Actor": "Actor1"
                }
            ]
        },
        {
            "Name": "Harry Potter and the Chamber of Secrets",
            "Actor": [
                {
                    "Actor": "Actor0"
                }
            ]
        }
    ]
},
{
 "Title_Book" : Another Book
  ... 
}

作为:

Title : Book
Volume[0][0] : Name of the book : Harry Potter and the Philosopher's Stone
Volume[0][1] : Actor 1
Volume[0][2] : Actor 1 
Volume[1][0] : Name of the book : Harry Potter and the Chamber of Secrets
Volume[1][1] : Actor 0 

Title1 ...
php json printing echo explode
1个回答
1
投票

使用json_decode,第二个参数设置为true。

示例代码:

$booksArray = json_decode($libraryJsonString,true);

foreach ($booksArray as $books) {

    print_r($books);

}

而不是print_r($booksArray);,您可以放置​​另一个foreach循环来遍历数组的每个元素。查看数组在解码后看起来如何正确使用:

$booksArray = json_decode($libraryJsonString,true);
print_r($booksArray);
© www.soinside.com 2019 - 2024. All rights reserved.