[英]How to access multidimensional JSON array in php
I am trying to build a web based file manager where I can view, create, edit, and delete folders and files. 我正在尝试构建一个基于Web的文件管理器,我可以在其中查看,创建,编辑和删除文件夹和文件。 I need a data structure for storing the information about the files folders and subfolders. 我需要一个数据结构来存储有关文件夹和子文件夹的信息。
I was trying JSON, but the problem is that there can be any number of subfolders inside subfolders which will come dynamically. 我正在尝试JSON,但问题是子文件夹中可以有任意数量的子文件夹,它们将动态出现。
I wrote by hand a JSON test file, which the link is given below 我手工编写了一个JSON测试文件,下面给出了链接
{
"home": {
"file1": {
"type": "file",
"name": "test",
"ext": "pdf",
"date": "12/03/2015"
},
"file2": {
"type": "file",
"name": "test",
"ext": "doc",
"date": "31/01/2010"
},
"folder1": {
"type": "folder",
"name": "folder1",
"date": "11/01/2010",
"in": {
"file": {
"type": "file",
"name": "test3",
"ext": "pdf",
"date": "23/01/2017"
},
"folder2": {
"type": "folder",
"name": "folder2",
"date": "22/03/2011",
"in": {
"file4": {
"type": "file",
"name": "test3",
"ext": "pdf",
"date": "23/01/2017"
}
}
},
"folder4": {
"type": "folder",
"name": "folder4",
"date": "11/09/2009",
"in": {
"file5": {
"type": "file:",
"name": "file5",
"date": "11/09/2011"
}
}
}
}
},
"folder7": {
"type": "folder",
"name": "folder7",
"date": "23/08/2015",
"in": {
"file7": {
"type": "file",
"name": "file7",
"date": "11/01/2016"
}
}
}
}
}
There will be a dynamic generation of folders and subfolders which needed to be updated in the JSON file. 将动态生成需要在JSON文件中更新的文件夹和子文件夹。
How can I access all the data from the JSON file after encoding into a variable in PHP or JavaScript, and update the file with new data as well? 如何在使用PHP或JavaScript编码变量后访问JSON文件中的所有数据,并使用新数据更新文件?
Thank you 谢谢
Read the file using file_open() 使用file_open()读取文件
Parse the json using json_decode 使用json_decode解析json
Do whatever you want with your arrays 用你的数组做任何你想做的事
encode the json again with json_encode 用json_encode再次编码json
write it to your file again with fwrite() 用fwrite()再次将它写入您的文件
Your code should look like this: 您的代码应如下所示:
<?php
$json = fread($myfile,filesize("json.txt"));
$json = json_decode($json);
//json is now an array, do whatever you want with it
$json = json_encode($json);
fwrite(file,json);
The below snippet should work, it basically does a json_decode($str, true)
of the JSON string : 下面的代码段应该可以工作,它基本上是JSON字符串的json_decode($str, true)
:
$str = '{
"type": "folder",
"name": "folder1",
"date": "11/01/2010",
"in": {
"file": {
"type": "file",
"name": "test3",
"ext": "pdf",
"date": "23/01/2017"
},
"folder2": {
"type": "folder",
"name": "folder2",
"date": "22/03/2011",
"in": {
"file4": {
"type": "file",
"name": "test3",
"ext": "pdf",
"date": "23/01/2017"
}
}
},
"folder4": {
"type": "folder",
"name": "folder4",
"date": "11/09/2009",
"in": {
"file5": {
"type": "file:",
"name": "file5",
"date": "11/09/2011"
}
}
}
}
}';
var_dump(json_decode($str, true));
Your code should look like this: 您的代码应如下所示:
<?php
$json = file_get_contents('json.txt'); // read the file contains JSON code
$array = json_decode($json); // convert json to normal php array
//json is now a normal php array, do whatever you want with it
echo '<pre>';
print_r($array);
echo '</pre>';
$json = json_encode($array);
file_put_contents('json.txt');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.