Apr 20, 2008

PHP Array Functions

PHP Array Functions

  • array() - Used to create an array
  • asort()
  • arsort()
  • count() - Counts the number of array items.
  • each()
  • ksort()
  • list() - Used to create an array
  • next() - Get the next array element
  • prev() - Get the previous array element
  • rsort()
  • sort()
  • uasort()
  • usort()
  • uksort()

Array_multisort Function

This function is used to sort multiple array entries. The example below reads a database then uses a search string called $searchvalue to search through entries in the database for matches. It uses the substr_count() function to count the number of matches. It uses the array_multisort() function to sort the results.

array_multisort($matchscore, $desc, $id, $url);

This example does not explain the use of the mysql database functions in detail, they are explained in more detail in later sections. The lines are also numbered for reference.

1.  $query1="select * from " . $mainsection;  //select all entries in the database
2.  $result = mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1);
3.  $nomatches=0;
4.  while($thisrow=mysql_fetch_row($result))  //get one row which is each entry for the section
5.  {
6.    $matches=0;
7.    $urlmatches=substr_count(strtolower($thisrow[1]),$searchvalue);  //url - 140
8.    $titlematches=substr_count(strtolower($thisrow[2]),$searchvalue);  //title
9.    $descmatches=substr_count(strtolower($thisrow[3]),$searchvalue);  //decription
10.   $matches=$urlmatches*3 + $titlematches*5 + $descmatches;
11.   $matchscore[$nomatches]=$matches;  //record the number of matches for each row in an array
12.   $url[$nomatches]=$thisrow[1];
13.   $title[$nomatches]=$thisrow[$2];
14.   $id[$nomatches]=$thisrow[0];
15.   $desc[$nomatches]=$thisrow[3];
16.   $nomatches++;
17.  }  //end while
18. array_multisort($matchscore, $desc, $id, $url);
19. for ($i5=$nomatches-1; $i5>=0; $i5--)  //get one row which is each entry for the section
20. {
21.   if ($matchscore[$i5] > 0)
22.   {
23.     echo "<table width=\"100%\" cellspacing=0 border=0 cellpadding=0 align=\"center\">";  //266
24.     echo "<tr><td width=\"90%\" bgcolor=\"#80b0ff\" bordercolor=\"#000080\" nowrap valign=\"top\">";
25.     echo "<font size=\"2\">Match Rank=" . $matchscore[$i5] . " </font><a href=\"$url[$i5]\">" . $title[$i5] . " </a>";
26.     echo "</td></tr>";
27.     echo "<tr><td width=\"90%\" valign=\"top\">";
28.     echo $desc[$i5] . "<br>";  //description
29.     echo "<font face=\"Arial\" size=\"2\"><i>";
30.     echo "</td></tr></table><br>";
31.   }  //end if $matchscoreɬ

32. }

Line 1 sets up the query for the database.
Line 2 reads the database.
Line 3 initializes the counter that tracks the number of matches.
Line 4 reads individual rows of the query results until all rows are read.
Lines 7-9 count the number of matches in each entry
Line 10 sets total match score which is weighed based on whether a match was found in the URL, title, and/or description.
Line 11 creates and places values in the array, $matchscore[], which is later used as a basis to sort the entries.
Lines 12-15 sets array values based on the match score and database entries.
Line 18 performs the sort of the array based on the matchscore first.
Line 19 sets up a loop to display the arrays in reverse order since the array was sorted from lowest to highest value.
Lines 22-31 display the matchstore, rl, url title, and description.

No comments:

Post a Comment

Popular Posts