Javascript 如何在php中访问多维JSON数组
我正在尝试建立一个基于web的文件管理器,在这里我可以查看、创建、编辑和删除文件夹和文件。 我需要一个数据结构来存储有关文件、文件夹和子文件夹的信息 我尝试了JSON,但问题是子文件夹中可以有任意数量的子文件夹,这些子文件夹将动态出现 我手工编写了一个JSON测试文件,下面给出了链接Javascript 如何在php中访问多维JSON数组,javascript,php,arrays,json,multidimensional-array,Javascript,Php,Arrays,Json,Multidimensional Array,我正在尝试建立一个基于web的文件管理器,在这里我可以查看、创建、编辑和删除文件夹和文件。 我需要一个数据结构来存储有关文件、文件夹和子文件夹的信息 我尝试了JSON,但问题是子文件夹中可以有任意数量的子文件夹,这些子文件夹将动态出现 我手工编写了一个JSON测试文件,下面给出了链接 { "home": { "file1": { "type": "file", "name": "test", "ext": "pdf", "date":
{
"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"
}
}
}
}
}
将动态生成需要在JSON文件中更新的文件夹和子文件夹
在编码成PHP或JavaScript变量后,如何访问JSON文件中的所有数据,并用新数据更新文件
谢谢使用
使用
对阵列执行任何操作
使用
用以下命令将其再次写入您的文件
您的代码应该如下所示:
<?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);
<?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');
使用
使用
对阵列执行任何操作
使用
用以下命令将其再次写入您的文件
您的代码应该如下所示:
<?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);
<?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');
下面的代码片段应该可以工作,它基本上对json字符串进行json\u解码($str,true)
:
下面的代码片段应该可以工作,它基本上对json字符串进行json_解码($str,true)
:
您的代码应该如下所示:
<?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);
<?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');
您的代码应该如下所示:
<?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);
<?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');
如何使用php访问json中的所有维度?一旦使用json_decode,您就可以将其作为一个数组使用。它看起来是这样的:$json['home']['file1']['type']如果我不知道其中的所有数据以及它包含了多少维度,如何访问它们?home、file1、type等是动态创建的。如果我不知道变量如何访问?那么你可以使用foreach迭代数组,这样你就可以访问单独的索引和键。我如何使用php访问json中的所有维度?一旦使用json_decode,你就可以将其作为一个数组使用。它看起来像这样:$json['home']['file1']['type']如果我不知道里面的所有数据是什么以及它进入了多少维度,如何访问它们?home、file1、type等是动态创建的。如果我不知道变量如何访问?那么您可以使用foreach迭代数组,这样您就可以在编码后访问单个索引和键。我如何从编码的变量访问数据?基本上,它类似于$array['in']['folder4']['in']['file5']['type']
编码后,如何从编码的变量访问数据?基本上,它类似于$array['in']['folder4']['in']['file5']['type']
Thanx用于回复我将尝试Thanx用于回复我将尝试这个Thanx