0

currently I use three separate php functions to array:

  1. folder names
  2. a thumbnail under the folder
  3. 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.

1
  • can you explain more about your question? Commented Feb 14, 2015 at 5:37

2 Answers 2

1

Read in all directories under folders/ and use them to set a new array:

<?php
$checkfolder = 'img/';
$json = [];

foreach ( glob($checkfolder . '*', GLOB_ONLYDIR) as $folder)
{
  $folderName = str_replace($checkfolder, '', $folder);
  $json[] = [
    'name' => $folderName,
    'thumbnail' => $folderName . '/thumb.svg',
    'text' => 'blah blah blah'
  ];
}
print_r(json_encode($json));

Inside of that loop you would do file existance checks, reading the text etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks! Here is a challenge to this solution though, the thumb should be $folder . "thumb.svg" so it turns out they look like this .\/folders\/folder1\/thumb.svg which is not exactly usable url for html...
I'd assemble the path for the image url when you need it using the fields name and thumb. No need to store the folder name a second time.
I see, so your mean creature a separate function to replace all the keys in thumb? How could I do that? Or do you mean I should create a new variable when I call the json? That seems to be even more complicated than my original solution (three separated json files).
Oh hey, I thought you might mean this. This solution is to simply add folder name to with /thumb.svg, which does not contain the full path.
0

in general is a text, so, if you want to make your script a JSON data source, you are able to print it out in that format.

For example:

echo "{\n";
echo "{\n";
echo "'name':'$folderArr[0]'\n";
echo "}\n";
echo "}\n";

However, you must get sure that there's no any other text printout will be occurred on the script to avoid corruption of JSON format.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.