katie hudson
katie hudson

Reputation: 2893

Creating directories based on array values

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

Upvotes: 0

Views: 63

Answers (1)

Antony D'Andrea
Antony D'Andrea

Reputation: 1004

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

Upvotes: 1

Related Questions