从本地文件夹的名称和文本文件中的数据在PHP上生成json数据
我想在PHP上输出jason数据,它看起来像下面的图片从本地文件夹的名称和文本文件中的数据在PHP上生成json数据,php,json,local,Php,Json,Local,我想在PHP上输出jason数据,它看起来像下面的图片folder是文件夹的名称,title位于info.txt的第一行。每个info.txt中有三行,这些行仅通过换行(而不是逗号)来分隔。文件夹books,我的php文件位于同一文件夹下。如何编写PHP代码?谢谢 我的PHP代码在这里 function books(){ $array = []; $story = glob("books/" . "*"); foreach($story as $each){
folder
是文件夹的名称,title
位于info.txt
的第一行。每个info.txt
中有三行,这些行仅通过换行(而不是逗号)来分隔。文件夹books
,我的php
文件位于同一文件夹下。如何编写PHP代码?谢谢
我的PHP代码在这里
function books(){
$array = [];
$story = glob("books/" . "*");
foreach($story as $each){
$title = file($each ."/info/txt", FILE_IGNORE_NEW_LINES);
$output = array (
"title" => $title[0],
"folder" => $each, #i would like to put the name of the folder here
);
array_push($array,$review);
}
print(json_encode($array));
}
要循环浏览所有文件夹,可以使用以下方法:
$dirs = array_filter(glob('*'), 'is_dir');
用于获取info.txt
文件
$books = array();
foreach($dirs as $dir){
$books[$dir] = file($dir)[0];
}
将JSON构建为关联数组并使用
使用保存JSON文件。
如何编写PHP代码?
打开编辑器并编写代码。哦,我完全不想把代码放在这里。。。。