douhuan1380 2016-09-24 20:14
浏览 36
已采纳

在子目录中找到编号最大的文件夹,并使用json进行处理

I have folder structure like below:

-Manga
   -Berserk
      -345
      -346
   -One Piece
      -840
      -841

And want JSON output like below:

{"0" => ["name":"Berserk", "last":"346"],
 "1" => ["name":"One Piece, "last":"841"]}

Like the title says I want JSON output that gives me every series name in manga folder and biggest numbered folder that belongs to that series.

I have something like this at the moment.

<?php 
$path = dirname(__FILE__) . "/../../manga"; 
$dir = new DirectoryIterator($path); 

$data = array("mangas"=> array()); 

foreach ($dir as $fileinfo) { 
    if (!$fileinfo->isDot() && $fileinfo->getFilename() !== ".DS_Store") { 
        array_push($data["mangas"], $fileinfo->getFilename()); 
    } 
} 

header('Content-Type: application/json; charset=UTF-8'); 
echo json_encode($data); 
?>

Edit: Slim Framework API

 $app->get('/ana', function ($request, $response, $args) {

 $path = "../../manga"; 
 function getAll($path)
 {
 $dirs = [];
 foreach (new DirectoryIterator($path) as $item) {
    if (!$item->isDir() || $item->isDot()) {
        continue;
    }
    if ($max = getMaxInDir($item->getRealPath())) {
        $dirs[] = [
            'name' => $item->getFilename(),
            'last' => $max,
        ];
    }
}
return $dirs;
}

function getMaxInDir($path)
{
$max = 0;
foreach (new DirectoryIterator($path) as $item) {
    $name = $item->getFilename();
    if (!$item->isDir() || $item->isDot() || !is_numeric($name)) {
        continue;
    }
    if ($current = (int)$name > $max) {
        $max = $current;
    };
}

return $max;
}
return $this->response->withJson(getAll($path));
});

Output of http://127.0.0.1/api/public/ana

[{"name":"Berserk","last":true},{"name":"One Piece","last":true}]
  • 写回答

1条回答 默认 最新

  • dongxingchang9345 2016-09-24 22:07
    关注

    First of all your desired output is not a valid JSON. It should rather be

    [
        {"name":"Berserk","last":346},
        {"name":"One Piece","last":841}
    ]
    

    Now one way to do it

    echo json_encode(getAll($path));
    
    function getAll($path)
    {
        $dirs = [];
        foreach (new DirectoryIterator($path) as $item) {
            if (!$item->isDir() || $item->isDot()) {
                continue;
            }
            if ($max = getMaxInDir($item->getRealPath())) {
                $dirs[] = [
                    'name' => $item->getFilename(),
                    'last' => $max,
                ];
            }
        }
        return $dirs;
    }
    
    function getMaxInDir($path)
    {
        $max = 0;
        foreach (new DirectoryIterator($path) as $item) {
            $name = $item->getFilename();
            if (!$item->isDir() || $item->isDot() || !is_numeric($name)) {
                continue;
            }
            if (($current = (int)$name) > $max) { // updated line
                $max = $current;
            };
        }
        return $max;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法