title: Get position of element in array created through mysql search - PHP
body: <p>I search a database that returns a bunch of values. These values are stored into an array called <code>$result_array</code>. I then want to find the position of an element within that array. Here is my current code</p>
  
  <pre><code>public function cardPosition($cid, $did) {
  
     $query  = "SELECT cid FROM card WHERE did = '$did' ORDER BY id ASC";
     $result = mysql_query($query);
     $result_array = array();
  
     while($row = mysql_fetch_assoc($result)) {
             $result_array[] = $row;
     }
  
     while ($correct_cid = current($result_array)) {
        if ($correct_cid == $cid) {
           $cardPosition = key($result_array);
        }
        next($result_array);
     }
  
     return $cardPosition;
  }
  </code></pre>
  
  <p>I use <code>mysql_fetch_assoc()</code> because I want to assign every element a key value. I then use the second while loop to search for the element in the array that is the <code>$correct_cid</code> value. I then assign the value of the key to <code>$cardPosition</code> but when I <code>return $cardPosition</code> I get nothing. How can I get the position of the element within the array?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have used </p>
  
  <pre><code>$position = array_search($cid, $result_array);
  </code></pre>
  
  <p>but still get nothing as a result.</p>
  
  <p>I know my query works because I ran</p>
  
  <pre><code>$num = mysql_num_rows($result);
  </code></pre>
  
  <p>and I get the correct number of rows.</p>