0

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 Answer 1

1

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

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

2 Comments

perfect. Is there any way to do the regex to make it 2016 so there is no month?
I actually see one more problem which means I may need the loops? I need to generate an image in the last directory that is created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.