Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need to do something for every element except either the first or the last and only if there is more than one element in the array, I prefer the following solution.</p> <p>I know there are many solutions above and posted months/one year before mine, but this is something I feel is fairly elegant in its own right. The check every loop is also a boolean check as opposed to a numeric "i=(count-1)" check, which may allow for less overhead.</p> <p>The structure of the loop may feel awkward, but you can compare it to the ordering of thead (beginning), tfoot (end), tbody (current) in HTML table tags.</p> <pre><code>$first = true; foreach($array as $key =&gt; $value) { if ($first) { $first = false; // Do what you want to do before the first element echo "List of key, value pairs:\n"; } else { // Do what you want to do at the end of every element // except the last, assuming the list has more than one element echo "\n"; } // Do what you want to do for the current element echo $key . ' =&gt; ' . $value; } </code></pre> <p>For instance, in web development terms, if you want to add a <strong>border-bottom to every element except the last</strong> in an unordered list (ul), then you can instead add a <strong>border-top to every element except the first</strong> (the CSS :first-child, supported by IE7+ and Firefox/Webkit supports this logic, whereas :last-child is not supported by IE7).</p> <p>You can feel free to reuse the $first variable for each and every nested loop as well and things will work just fine since every loop makes $first false during the first process of the first iteration (so breaks/exceptions won't cause issues).</p> <pre><code>$first = true; foreach($array as $key =&gt; $subArray) { if ($first) { $string = "List of key =&gt; value array pairs:\n"; $first = false; } else { echo "\n"; } $string .= $key . '=&gt;('; $first = true; foreach($subArray as $key =&gt; $value) { if ($first) { $first = false; } else { $string .= ', '; } $string .= $key . '=&gt;' . $value; } $string .= ')'; } echo $string; </code></pre> <p>Example output:</p> <pre><code>List of key =&gt; value array pairs: key1=&gt;(v1_key1=&gt;v1_val1, v1_key2=&gt;v1_val2) key2=&gt;(v2_key1=&gt;v2_val1, v2_key2=&gt;v2_val2, v2_key3=&gt;v2_val3) key3=&gt;(v3_key1=&gt;v3_val1) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload