title: How do I optimise O(n^2)
body: <p>I am having an array a1[] of size more than 1000. Each array elements contains array of size 100 or more.</p>
  
  <p>I am looping first array a1 and within the loop I am again looping elements of a1.</p>
  
  <p>This is slow and probably is O(nm). </p>
  
  <p>How can I optimise this ?</p>
  
  <pre><code>foreach ($a1 as $key =&gt; $values) {
       foreach ($values as $k =&gt; $v) {
        $obj-&gt;insertIntoTable($v); // This is a db operations it takes time too
   }
  }
  </code></pre>