title: Iterating an associative array without the use of foreach loop and causing memory leaks
body: <p>I am developing a command line script which runs in an infinite loop. After a while it causes a segmentation fault which I think is caused by memory leaks. I think I am correct, because after looking on the results produced by the <code>ps</code> command it looks like the memory used by the script constantly increases before the script crashes.</p>
  
  <p>I have found <a href="http://wildness.espix.org/index.php?post/2011/05/09/PHP5%3a-Memory-leak-with-object-arrays" rel="nofollow">this article</a>, which states that one possible cause of memory leaks in command line php is the use of <code>foreach</code> loops, which create copies of arrays that are never unset. After some research it looks like that is the case. So I decided to replace all <code>foreach</code> loops with their <code>for</code> equivalents.</p>
  
  <p>First question - is my reasoning correct?</p>
  
  <p>Second one - what to do if I have an associative array to iterate and I would like to know the current key?</p>
  
  <p>One way I can think of would be using <code>array_walk()</code>, other one - using combination of <code>next()</code> and <code>key()</code> functions in a <code>for</code> loop. Which approach wouldn't leave me with memory leaks?</p>
  
  <p>I will perform some tests and post the results after I'm done.</p>
  
  <p>Secondary problem would be how to deal with iterable objects but that's for later.</p>
  
  <p><strong>EDIT 1</strong>: There are some differing results from my tests so I'll post something new after some more research.</p>