当通过php将它们排列成数组中的数组时,如何重组json数组并为每个键提供键字符串? [英] How could I reorganize json arrays and give key string to each key while arraying them into arrays within an array through php?

查看:125
本文介绍了当通过php将它们排列成数组中的数组时,如何重组json数组并为每个键提供键字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用三个单独的php函数进行数组:

currently I use three separate php functions to array:

  1. 文件夹名称
  2. 文件夹下的缩略图
  3. 文件夹下的文本文件

分成三个分离的json文件,因此它们现在是:

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"]

这对我来说很好用,但是如果我可以将它们变成一个像这样的json文件,那就容易多了:

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"
  },
]

有办法吗?谢谢.

详细说明:

例如,我尝试了array("name" => array_map("basename", glob('./folders/*', GLOB_ONLYDIR)),),它只是将我所有的文件夹作为一个巨型数组放在名称"的单个条目下,例如{"name":["folder1","folder2","folder3"]}.

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"]}.

伪解决方案:

IkoTikashi 提供了一种解决方案,虽然不必回答这个问题,但对某些人可能有用.我用他的想法在下面建立了一些示例代码:

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);

?>

当然,这种解决方案不是完美的.对于其中一个,它不提供缩略图的可用网址.更重要的是,它消除了原始代码的模块化-用户可以使用三个独立的api来生成所需的特定json.重组现有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.

推荐答案

读取folders/下的所有目录,并使用它们来设置新数组:

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.

这篇关于当通过php将它们排列成数组中的数组时,如何重组json数组并为每个键提供键字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆