title: How to create php array from mysql result, perform calculation, resort array, present results
body: <p>I have a photo website on which I am trying to perform a query against a MySQL database. The query is against a concatenated field of 'title' and 'keyword' called 'title_keyword'.
  I want to take the search results and sort them by a newly formed variable called 'sort_priority' which is checking to see if the search word is in the 'title' field. If it is in the 'title' field then I want to assign a value of 1 and if not in the title field then a value of 2. The resulting array will be sorted by 'sort_priority' and output to the screen.</p>
  
  <p>Here is the logic I am using with PHP and MySQL:</p>
  
  <p>1) Query the MySQL database and assign variables. (This works just fine)</p>
  
  <p>2) Take the results, assign each field to a variable, create a new variable that performs a calculation on one of the variables returned</p>
  
  <pre><code>$data_array=array();
  // get each row
  while($row = mysql_fetch_array($result))
  {
  //get data
  $image_id = "{$row['image_id']}";
  $title = "{$row['title']}";
  $imageurl = "{$row['imageurl']}";
  
  
  // Create sort_priority to identify if search word is in title field. 
  //If it is then set to 1 to force this higher in the result list after sorting
  
  $sort_priority = 2;
  if(stristr($title,$search))
  { $sort_priority = 1;}
  </code></pre>
  
  <p>Everything above this point works. Now for the part I'm stumped on. How to create and add data to the array and then sort on my new $sort_priority variable.</p>
  
  <p><em><strong></em></strong><em>Here is what I've written but it just doesn't work</em><strong>**</strong></p>
  
  <pre><code>// Create array and sort by title then keyword (tk_sort)
  $data_array = array(
  'image_id' =&gt; $image_id, 
  'title' =&gt; $title, 
  'imageurl' =&gt; $imageurl, 
  'sort_priority' =&gt; $sort_priority);
  
  // Obtain a list of columns
  foreach ($data_array as $key =&gt; $row) {
      $image_id[$key]  = $row['image_id'];
      $title[$key] = $row['atitle'];
      $imageurl[$key]  = $row['imageurl'];
      $sort_priority[$key] = $row['sort_priority'];
  }
  
  // Sort the data with volume descending, edition ascending
  // Add $data as the last parameter, to sort by the common key
  
  array_multisort($sort_priority, SORT_ASC);
  
  // end of array creation and sort
  </code></pre>
  
  <p>3) Output the newly sorted array to a table</p>
  
  <p>Not sure how to get the data out of it. Do I have to use a loop or something?</p>