I have an associative array of the format:
$assocarray1 = array( 'key1' => array(0,0,0), 'key2' => array(0,0,0), 'key3' => array(0,0,0), );
Then I have a big complicated function (using a foreach loop with while loops nested in it…yeah it’s a bit of a mess) that runs a bunch of queries on a database recursively, adds up some numbers which are stored in the array values and updated after each iteration, and then prints them in a table. All that works great so far.
Then, directly afterwards in the same function, I try to write the data to a CSV (again using a foreach loop), which seems to be working fine EXCEPT all the values in my arrays are now zero again, even though the numbers were successfully added up and displayed earlier! It’s as if the values reverted to their initial values after leaving the first foreach loop. Does a foreach loop use a separate instance of an array instead of the original array or something? Both foreach loops and the array are in the body of the same function (again kind of messy but for a good reason, the program has to be highly modular…long story)
I’m sort of new to PHP although I’m pretty comfortable with it now and I’m an experienced programmer. Any ideas?
Edit: I’ve done some more testing and it’s definitely pulling the initial values verbatim, although the values are maintained within a foreach loop. I can only conclude that a foreach loop uses a separate instance of the array. If the array declaration in PHP is only a definition, then is there some way to create a separate instance first and then pass it to my foreach loops? If not, I guess I could try defining a new associative array like the original based on the result of my first foreach loop, and then passing it to the second one, but that would be a lot of extra work