title: Repeating if/elseif within a foreach loop (PHP)
body: <p>I have a series of <code>if</code>/<code>elseif</code> conditional statements using the same structure and syntax, and I'd like to clean up and condense the code within a <code>foreach</code> to more easily add and remove items as the list grows. What I'm starting with looks something like this:</p>
  
  <pre><code>if ( $condition1 == 1 &amp;&amp; ( $condition2 == 1 || $condition3 == 1 ) ) {
    $var = 'string 1';
  } elseif ( $condition1 == 2 &amp;&amp; ( $condition2 == 2 || $condition3 == 2 ) ) {
    $var = 'string 2';
  } elseif ( $condition1 == 3 &amp;&amp; ( $condition2 == 3 || $condition3 == 3 ) ) {
    $var = 'string 3';
  } elseif ( $condition1 == 4 &amp;&amp; ( $condition2 == 4 || $condition3 == 4 ) ) {
    $var = 'string 4';
  } else {
    $var = 'default string';
  }
  </code></pre>
  
  <p>Ideally, I would structure this more like:</p>
  
  <pre><code>$conditions = array(
    'string 1'    =&gt; 1,
    'string 2'    =&gt; 2,
    'string 3'    =&gt; 3,
    'string 4'    =&gt; 4,
  );
  foreach ( $conditions as $key =&gt; $value ) {
    if ( $condition1 == $value &amp;&amp; ( $condition2 == $value || $condition3 == $value ) )
      $var = $key;
  }
  if ( empty($var) )
    $var = 'default string';
  </code></pre>
  
  <p>The biggest issue here is that I want all <code>if</code> statements  after the first iteration to be <code>elseif</code> to in case multiple conditions are met so I can control the hierarchy by the order of the items in the array. Also, this approach may be totally wrong and there could be a much better way to approach the problem altogether, so I'm open to any and all critique!</p>