【发布时间】:2018-10-25 15:51:21
【问题描述】:
我正在构建一个基于 cms 的小文件夹。我正在使用 php 来提取文件夹的所有路径,并且我想使用 javascript 来构建网站。我知道我可以用 php 做所有事情,但我更流利地使用 javascript,因此我想使用它而不是 php。无论如何,使用 php 我得到了这个列表:
0: {parent: "theater", child: "How_to_be_a_regisseur", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in."}
1: {parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/download.jpeg"}
2: {parent: "theater", child: "How_to_be_a_regisseur", img: "./content/theater/How_to_be_a_regisseur/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"}
4: {parent: "theater", child: "WOOWW", text: "Project 1!↵Phasellus a quam non arcu accumsan rhon…ium leo ante, vitae volutpat diam consectetur in."}
...
12: {parent: "varia", child: "gggggg", text: "Project 1!↵Phasellus a
13: {parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/download.jpeg"}
14: {parent: "varia", child: "gggggg", img: "./content/varia/gggggg/img/rsz_namibia_will_burrard_lucas_wwf_us_1.jpg"}
...
20: {parent: "about", child: null, img: "./content/about/img/download.jpeg"}
parent 表示文件夹,child 表示子文件夹,img 或text 表示该文件夹的内容。
有没有办法将此数组重组为具有此结构的对象数组,其中所有parents 都成为包含children 数组的属性,每个数组都包含带有图像或文本链接的不同对象?
{
theater: [
How_to_be_a_regisseur: {
img: "path",
text: "blah blah"
},
WOOW: {
img: "path",
text: "blah blah"
}
],
varia:[...{[][]}]
}
【问题讨论】:
-
There's no such thing as a "JSON object"。那只是一个对象数组。
-
在 PHP 中按照您希望在 JavaScript 中表示的方式构建您的对象,并使用
json_encode将对象序列化为 JSON,然后在您的 JS 中使用它。
标签: javascript php arrays json content-management-system