title: Foreach: Get All The Keys That Have The Value "X"
body: <p>Suppose I have an array like this:</p>
  
  <pre><code>$array = array("a","b","c","d","a","a");
  </code></pre>
  
  <p>and I want to get all the keys that have the value "a".</p>
  
  <p>I know I can get them using a <code>while</code> loop:</p>
  
  <pre><code>while ($a = current($array)) {
      if ($a == 'a') {
          echo key($array).',';
      }
      next($array);
  }
  </code></pre>
  
  <p><strong>How can I get them using a <code>foreach</code> loop instead?</strong></p>
  
  <p>I've tried:</p>
  
  <pre><code>   foreach ($array as $a) {
          if ($a == 'a') {
              echo key($array).',';
          }
  
      }
  </code></pre>
  
  <p>and I got </p>
  
  <blockquote>
    <p>1,1,1,</p>
  </blockquote>
  
  <p>as the result.</p>