处理数组以重新生成新数组

I make a call to an API and receive a JSON response which I then decode into an Array. The data I end up with is something 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
                                )

                        )

                )

        )
)

Because I am doing this in a loop, I receive many responses like the above. My aim is to extract the values from labelValues so I can create a folder structure.

Essentially, the above will generate an image, and for the above example the image should be placed in the following directory structure

2016 > Food > Rice > the generated image

I can handle creating the folder structure, but first I need to place the above data into an array. I would expect the array to look something like this

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

    [1] => Array
        (
            [Date] => second_loop_date
            [Title] => second_loop_title
            [Product] => second_loop_product
        )
    ...
)  

I have been trying to take it step by step to get the desired output, and I have been print things in order to debug along the way. I am sure there is a more effecient way compared to what I have done, but so far I have something like this

foreach ($output['options'] as $key => $value) {
    if($key == 'data') {
        foreach ($value as $label) {
            foreach ($label as  $labelValue) {
                foreach($labelValue as $output) {
                    print_r("<pre>");
                    print_r($output);
                    print_r("</pre>");
                }
            }
        }
    }
}

This kind of gets me to the values, but I also get a lot of warnings like

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\index.php on line 43

So really I was wondering what the best way to create this array would be. I have seen array_search, but not too sure if I can use it in this situation?

Any advice appreciated

I think that you a little bit complicate your code. You can access directly $output['options']['data'] and it let you remove two outer loops.

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

Note: I simplified accessing labelValues, because it looks form your example array, that there is always only one item. In other case, you should use your inner loop to iterate over all labelValues and concatenate them into one string perhaps.

Your current loop seems a little bit complicated..

The error occurs because you want to iterate over an string(labelName), which isn't possible.

Transform your current loop to a working one, should look like this:

foreach ($output['options']['data'] as $item) {
    print_r("<pre>");
    print_r($item['labelValues'][0]);
    print_r("</pre>");
}

The solution using array_column and array_combine functions:

...
$result = array_combine(
    array_column($output['options']['data'], 'labelName'),
    array_column(array_column($output['options']['data'], 'labelValues'), 0)
);

print_r($result);