dongxibeng5324 2017-01-18 11:05
浏览 17

根据数组值创建目录

I get a response from an API call that looks like the following

Array
(
    [_id] => aasdasdasdasdasd
    [created] => 2017-01-16T14:11:54.616Z
    [options] => Array
        (
            [title] => 1
            [data] => Array
                (
                    [0] => Array
                        (
                            [labelName] =>  Date
                            [labelValues] => Array
                                (
                                    [0] => March 2016
                                )

                        )

                    [1] => Array
                        (
                            [labelName] => Title
                            [labelValues] => Array
                                (
                                    [0] => Food
                                )

                        )

                    [2] => Array
                        (
                            [labelName] => Product
                            [labelValues] => Array
                                (
                                    [0] => Rice
                                )

                        )

                )

        )
)

I then process the response by doing the following

$results = array();
foreach ($output['options']['data'] as $data) {
    if (isset($data['labelValues'][0])) {
        $results[$data['labelName']] = $data['labelValues'][0];
    }
}

This leaves me with something along the lines of this

Array
(
    [Date] => March 2016
    [Title] => Food
    [Product] => Rice
)

Putting it into an array was not my intention, it was mainly done to help me better understand the looping required to process the original data.

My main intention is to create directories from these values. The main directory will be the Date, within this should be the Title and within this should be Product. So for the above, the directory structure should be something like 2016 > Food > Rice.

In order to achieve this, I have come up with the following

foreach ($output['options']['data'] as $data) {
    if (isset($data['labelValues'][0])) {
        if($data['labelName'] == 'Date') {
            if (preg_match('/\b\d{4}\b/', $data['labelValues'][0], $matches)) {
                $results[$data['labelName']] = $matches[0];
                if (!file_exists($matches[0])) {
                    mkdir($matches[0], 0777, true);
                }
            }
        }
    }
}

The above works well and creates the date folder for me. Where I am struggling is how I now create the Title folder within the Date folder, and then the Product within the Title.

How would I go about achieving this?

Many thanks

  • 写回答

1条回答 默认 最新

  • dsaj20411 2017-01-18 11:17
    关注

    I like your created array:

    $array = array ( [Date] => March 2016 [Title] => Food [Product] => Rice )

    Simply implode the path:

    mkdir(implode('/', $array), 0777, true);
    

    This will create all of the directories, March 2016/Food/Rice

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度