title: PHP - when using foreach to loop through array, how can I tell if I'm on the last pair?
body: <p>I'm trying to 'pretty-print' out an array, using a syntax like:</p>
  
  <pre><code>$outString = "[";
  foreach($arr as $key=&gt;$value) {
      // Do stuff with the key + value, putting the result in $outString.
      $outString .= ", ";
  }
  $outString .= "]";
  </code></pre>
  
  <p>However the obvious downside of this method is that it will show a "," at the end of the array print out, before the closing "]". Is there a good way using the $key=>$value syntax to detect if you're on the last pair in the array, or should I switch to a loop using <code>each()</code> instead?</p>