


I just don't know if I'm setting myself up for some weird bugs or CPU hits later on.
Foreach php optmize code#
The code runs fine, and so far my datasets have been too negligible in size to test for performance differences. Is the call to unset() a good idea here? I.e., does it conserve memory since I have copied the data and no longer need the original value? Or, does PHP automatically garbage collect the data since I don't reference it in any subsequent code? do some formatting here, copy to $reformattedVal So, during my foreach loop I copy the current array value(s) into the new format, then I unset the value I'm working with from the original array. My question lies in finding a way to conserve memory since these arrays might get a little unwieldy. I'm totally fine with my formatting algorithm. So I take the first $myData and format it like the second $myData. A simple example would be: $myData = array('Key1' => array('value1', 'value2')) īut what I want would be something like: $myData = array( => array('MyKey' => array('Key1' => array('value1', 'value2')))) It's like this, I have an array that comes to me from an outside source formatted in a way I want to change. This is a simple programming question, coming from my lack of knowledge of how PHP handles array copying and unsetting during a foreach loop.
