Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从本地文件夹的名称和文本文件中的数据在PHP上生成json数据_Php_Json_Local - Fatal编程技术网

从本地文件夹的名称和文本文件中的数据在PHP上生成json数据

从本地文件夹的名称和文本文件中的数据在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){

我想在PHP上输出jason数据,它看起来像下面的图片
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代码?
打开编辑器并编写代码。哦,我完全不想把代码放在这里。。。。