title: Efficiently Checking for matching values in an Array?
body: <p>I have an array like this one :</p>
  
  <pre><code>$pref_array = ('android/cbnj','android/fgngh','android/vfjnf','apple/jns','apple/dvjnvd','pictues/djnf','pictures/djnf','pictures/hjf','pictures/cvjbv');
  </code></pre>
  
  <p>Number of elements that have <code>android/</code> in them can be anywhere between 0 to N, where N is size of array. Now, I have to find if a value like 'android/<code>or 'buffalo/</code> etc. exists in any of these elements. If it exists I do some stuff otherwise I check for other values. Here is my code :</p>
  
  <pre><code>$cCount = count($pref_array);
  for($j=1;$j&lt;$cCount;$j++) {
    $code = $pref_array[$j];
    if(strpos($code, 'android/') !== false) { 
      // Do Android Related Stuff just once.
      $j = $cCount;  
    } 
  }
  for($j=1;$j&lt;$cCount;$j++) {
    $code = $pref_array[$j];
    if(strpos($code, 'buffalo/') !== false) { 
      // Do Buffalo Related Stuff just once.
      $j = $cCount;  
    } 
  }
  for($j=1;$j&lt;$cCount;$j++) {
    $code = $pref_array[$j];
    if(strpos($code, 'apple/') !== false) { 
      // Do Apple Related Stuff just once.
      $j = $cCount;  
    } 
  }
  </code></pre>
  
  <p>and so on. It works but I don't find it efficient enough. Each time I am iterating through the array from the beginning. Is there any way I can iterate through the array just once?</p>
  
  <p>I am setting <code>$j = $cCount</code> to break out of loop.  </p>