title: php foreach supplied argument not valid
body: <p>I am getting results from a mysql table and putting each cell into an array as follows:</p>
  
  <pre><code>$sqlArray = mysql_query("SELECT id,firstName FROM members WHERE id='$id'");
  while ($arrayRow = mysql_fetch_array($sqlArray)) {
      $friendArray[] = array(
      'id' =&gt; $arrayRow['id'],
      'firstName' =&gt; $arrayRow['firstName'],
      );          
  }
  </code></pre>
  
  <p>Then I do a search for a specific friend. For example if I want to search for a friend name <em>Osman</em>, i would type and <code>o</code> and it will return to me all the results that start with the letter <code>o</code>. Here is my code for that:</p>
  
  <pre><code>function array_multi_search($array, $index, $pattern, $invert = FALSE) {
      $output = array();
      if (is_array($array)) {
          foreach($array as $i =&gt; $arr) {
              // The index must exist and match the pattern
              if (isset($arr[$index]) &amp;&amp; (bool) $invert !== (bool) preg_match($pattern, $arr[$index])) {
                  $output[$i] = $arr;
              }
          }
      }
      return $output;
  }
  $filtered = array_multi_search($friendArray, 'firstName', '/^o/i');
  </code></pre>
  
  <p>and then it will print out all the results. My problem is that it returned an error saying <code>"Invalid argument supplied to foreach()"</code> and that is why I added the <code>if(is_array))</code> condition. It is working fine if I leave this code in the <code>index.php</code> page, but I moved it to a subfolder named <code>phpScripts</code> and it doesn't work there. Any Help?</p>
  
  <p><code>$output</code> is not returning any value because apparently <code>$friendArray</code> is not an array. But I verified that it is by using <code>print_r($friendArray)</code> and it returns all the member's <code>id</code> and <code>firstName</code>.</p>
  
  <p>P.S. I use JavaScript to the the call using AJAX.</p>