title: How to handle very large arrays in php?
body: <p>I have this block of code.</p>
  
  <pre><code>$input = 4;
  $list_N = array('0', '1');
  for($n=1; $n&lt;=$input; $n++) {
  
      if($n%2 == 0) {
          $c++;
      }
  
      $reverse_list_N = array_reverse($list_N);
  
      $A = array();
      $B = array();
  
      for($i=0; $i&lt;count($list_N); $i++) {
          $A[] = '0' . $list_N[$i];
          $B[] = '1' . $reverse_list_N[$i];
      }
  
      $list_N = array_merge($A[], $B[]);
  
      if($n == 1) {
          $list_N = array('0', '1');
      }
  }
  $array_sliced = array_slice($list_N, -1*$input, $input);
  for($i=0; $i&lt;count($array_sliced); $i++) {
      $output = implode("\n", $array_sliced);
  }
  echo "&lt;pre&gt;"; print_r($output); echo "&lt;/pre&gt;";
  </code></pre>
  
  <p>What this code does is, it generates following data (starting from (0,1)):</p>
  
  <pre><code>0,1
  00, 01, 11, 10
  000, 001, 011, 010, 110, 111, 101, 100
  ....... and so on
  </code></pre>
  
  <p>When <code>$input = 4;</code> the output is:</p>
  
  <pre><code>1010
  1011
  1001
  1000
  </code></pre>
  
  <p>And as you can see, after every loop the the elements in the <code>$list_N</code> array doubles than the previous one. And with this pace if <code>$input = 25;</code> then the array would have <code>33554432</code> elements which is very huge. And that is the problem I couldn't find a solution of. When <code>$input = 60</code> I get this error</p>
  
  <pre><code>Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes)
  </code></pre>
  
  <p>on this line</p>
  
  <pre><code>$list_N = array_merge($A, $B);
  </code></pre>
  
  <p>Even setting the memory limit to 2G did not solve it. So, how do I optimize my code in order to same the memory. Or, is there any other solution ?</p>
  
  <p>Update: Following steps are used to generate the data.</p>
  
  <pre><code>$list_N is an array
  $reverse_list is the reverse of $list_N
  0 is appended in the beginning of every element in the $list_N array and stored in $A
  1 is appended in the beginning of every element in the $reverse_list_N array and stored in $B
  Array $A and array $B are merged and is stored in $list_N.
  The main loop runs for $input number of times and the last $input number of elements are displayed from the final array.
  </code></pre>