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