PHP nested array processing to create date keys and values

2 answers

I have a PHP project where I manage data structures (in a $data variable) defined like this:

array:35 [▼   "2020-03-16" => array:12 [▼     "00:00:00" => 17.185     "02:00:00" => 17.235     "04:00:00" => 17.25     "06:00:00" => 16.905     "08:00:00" => 21.205     "10:00:00" => 23.47     "12:00:00" => 24.42     "14:00:00" => 27.245     "16:00:00" => 30.94     "18:00:00" => 30.78     "20:00:00" => 29.735     "22:00:00" => 28.64   ]   "2020-03-17" => array:12 [▼     "00:00:00" => 29.985     "02:00:00" => 32.455     "04:00:00" => 31.28     "06:00:00" => 34.33     "08:00:00" => 29.755     "10:00:00" => 26.92     "12:00:00" => 29.72     "14:00:00" => 28.36     "16:00:00" => 30.815     "18:00:00" => 34.605     "20:00:00" => 34.315     "22:00:00" => 33.125   ]   "2020-03-18" => array:12 [▶] ... ] 

They come from an external service so I can't modify them in origin. I need to "rewrite" $data so that I finally have something like:

array:420 [▼ "2020-03-16 00:00:00" => 17.185 "2020-03-16 02:00:00" => 17.235 "2020-03-16 04:00:00" => 17.25 ... "2020-03-20 00:00:00" => 22.675 "2020-03-20 02:00:00" => 24.67 ... ] 

Could you please tell me a smart way of doing that? Many thanks in advance.

All answers to this question, which has the identifier 61238755

The best answer:

Just working with data structures, I found the solution:

$cleanData = []; foreach ($data as $day => $arr) {     foreach ($arr as $hour => $value)         $cleanData[$day.' '.$hour] = $value; } 

Some enhancement or any smarter way? What do you think about the solution? Thanks again.

I think that code below is simple to understand thanks to one loop :)

$iterator = new RecursiveArrayIterator($inputArray); $nestedIterator = new RecursiveIteratorIterator($iterator);  $output = []; foreach($nestedIterator as $nestedKey=>$value) {     $output["{$iterator->key()} {$nestedKey}"] = $value;  } unset($inputArray); // if you need print_r($output); 

ofc, two loops often are smart and clear.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data