currently I use three separate php functions to array:
- folder names
- a thumbnail under the folder
- a text file under the folder
into three separated json files, so they now are:
["folder1","folder2","folder3"]
["folder1/thumb.svg","folder2/thumb.svg","folder3/thumb.svg"]
["blah blah blah","blah blah blah","blah blah blah"]
This works fine for me but it would be so much easier if I can make them into one json file looks like this:
[
{
"name" : "folder1",
"thumbnail" : "folder1/thumb.svg",
"text": "blah blah blah"
},
{
"name" : "folder2",
"thumbnail" : "folder2/thumb.svg",
"text": "blah blah blah"
},
{
"name" : "folder3",
"thumbnail" : "folder3/thumb.svg",
"text": "blah blah blah"
},
]
Is there a way to do it? Thanks.
Explain more:
For example I tried array("name" => array_map("basename", glob('./folders/*', GLOB_ONLYDIR)),) and it just put all my folders as a giant array under one single entry of "name," like this {"name":["folder1","folder2","folder3"]}.
A pseudo solution:
IkoTikashi provided a solution, while not necessary answering the question, but could be useful to some people. I use his idea to establish some example codes below:
<?php
$checkfolder = './path/examples/folders';
$json = [];
foreach ( glob($checkfolder . '*', GLOB_ONLYDIR) as $folder)
{
$filename = $folder . "/description.txt";
if (file_exists($filename)) {
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
} else {
$contents = '';
}
$json[] = [
'name' => str_replace($checkfolder, '', $folder),
'thumb' => $folder . '/thumb.svg',
'text' => $contents
];
}
json_encode($json);
?>
Of course this solution isn't perfect. For one it doesn't provide usable url for thumbnails. And more importantly it erased the modularity of the original codes - that users can use three separated api to generate specific json to their need. The reason to reorganize the existing json files is that they can have an additional option to generate combined arrays. This solution, rather, created a whole new function to accomplish such goal - so while it is a temporary solution, it lacks of upgradability.