title: Looping through difference sized arrays - avoiding 'Undefined offset' error
body: <p>I'm working on a basic system to generate random word combinations.</p>
  
  <p>I run one bit of SQL and get an array with results in, and another bit of SQL to get a 2nd array with a different set of words.</p>
  
  <p>I loop through the first array, and join the first word from that with the corresponding word from the 2nd array.</p>
  
  <p>Therefore, say I have these two arrays:</p>
  
  <pre><code>Array // referred to as `$list1
  
  (
      [0] =&gt; yttrium
      [1] =&gt; yafty
      [2] =&gt; yard
      [3] =&gt; yellowknife
      [4] =&gt; yeasty
      [5] =&gt; york
      [6] =&gt; yo
      [7] =&gt; yemeles
      [8] =&gt; yo
      [9] =&gt; yellowstone
  )
  
  Array // referred to as `$list2
  
  (
      [0] =&gt; yudding
      [1] =&gt; yudge
      [2] =&gt; yoody
      [3] =&gt; yeer
      [4] =&gt; yoisy
      [5] =&gt; yafty
      [6] =&gt; yine
      [7] =&gt; yuity
      [8] =&gt; yio
  )
  </code></pre>
  
  <p>I loop through using this basic bit of code:</p>
  
  <pre><code>foreach ($list1 as $k =&gt; $v) {
  
      $words['output'][] = [
          'id' =&gt; $k,
          'value' =&gt; $v . '.' . $list2[$k]
      ];
  }
  </code></pre>
  
  <p>Looking at the sample array above, the problem I have is if there are more elements in <code>list1</code> compared to in <code>list2</code> as I get this error:</p>
  
  <pre><code>Undefined offset: 9 in C:\xampp\public_html\test.php on line 224
  </code></pre>
  
  <p>Where line 224 is this one:</p>
  
  <pre><code>'value' =&gt; $v . '.' . $list2[$k]
  </code></pre>
  
  <p>I have been trying to work out how to get around this - for example - by counting the size of the arrays, and if <code>list2</code> contains fewer elements than <code>list1</code> then only loop through <code>list1</code> as many times as there are elements in <code>list2</code>.</p>
  
  <p>I was trying klunky solutions like:</p>
  
  <pre><code>$s1 = sizeof($list1);
  $s2 = sizeof($list2);
  
  if ($s2 &lt; $s1) {
      $k = $s2;
  } else {
      $k = $s1;
  }
  
  foreach ($list1 as $k =&gt; $v) {
  
      $words['output'][] = [
          'id' =&gt; $k,
          'value' =&gt; $v . '.' . $list2[$k]
      ];
  }
  </code></pre>
  
  <p>However, that doesn't solve the problem.</p>