Pigmaster;11034251 wrote:
and only trying to get things working with thought processes is difficult due to my brain fog.
The more you practice creating data traversal algorithms, the easier it gets. All you really need is repetition, and if you keep on programming, you are likely to be doing this intuitively in the end. But I strongly believe that doing as much as possible of the reasoning yourself helps furthering your understanding in this regard a lot faster, than you would by being given the code.
If you ever get stuck creating algorithms, try formulating what you need to do in words. And do formulate it very precisely. It's the same principle as writing cooking instructions / recipies.
The problem is that you are doing the following
1. For every date in the xml file do all steps below
1.1 For every location period in the entire xml file do all steps below (i.e. once per report date you will be processing all periods in the entire file)
1.1.1 For all reports in the entire file do all steps below (i.e. once for every combination of report date and period, process all reports in the entire file)
While what you want is
1. For every report date in the xml file
1.1 Go through all of [b]the currently processed report date's[/b] periods
1.1.1 Go through all of [b]the currently processed period's[/b] reports
This is the best explanation I can come up with relating to your problem. If you understand the difference, you should be able to sort it out yourself at this point. Hint 1: you only need to change what you foreach over in the two inner loops.
Hint2: "For every date in the xml file" is written as
foreach ($simple_xml_obj->DV as $datadate)
And $datadate is the report date currently being processed
Rephrasing Weedpacket's question first into a statement while adding the same comment for the outer loop
You should be iterating over $datadate instead of iterating over $simple_xml_obj->DV every time.
You should be iterating over $lper instead of iterating over $simple_xml_obj->DV->Location->Period every time
Our use of variable names is not random or similar, it's identical because we are referring to your code. Thus, once again, take a look at
foreach($simple_xml_obj->DV as $datadate) {
foreach($simple_xml_obj->DV->Location->Period as $lper) {
Expressed in the form of a question: Where do you think you could be iterating over $datadate instead of $simple_xml_obj->DV?
And can you explain, in words, what the difference between the two is?
And the same thing relating to the other loop: Where do you think you could be iterating over $lper instead of $simple_xml_obj->DV->Location->Period?
The difference between these two is the exact same as the difference of the previous pair.