SEO Services
Home >>
Webmaster Forums >>
multidimensional array searchmultidimensional array search
<h1> said: "I wrote this script for a friend who wanted the simplest of searches for a site he was working on.
Basically you specify the url, title, description and keywords in a multi-dimensional array.
The searched words are then compared to the entires in the array and returns the url, title and description of the found entries.
This search can be edited slightly to to a full text search. All you really need to do is place the text you want to search in an array...so if you know what you doing you could enter the contents of say a .html file into an array and search it...;) TELL me what u think.
[PHP]
// Lets make a "fake" search.
// If the searchstring is found in the multi-dimensional array then return
// the page url, title and description which is also stored in the multi-dimensional array
// The searchstring can be matched more than once.
// 2 stage script. 1st display form, 2nd display results
$searcharray = array(
array('home.html','Home','The homepage, this page contact menu and cotact details','home','contact'),
array('products.html','Products','All the products are shown in this page','products','caps','shirts','home'),
array('guestbook.html','The guestbook','sign out guestbook or just surf the current entires','guestbook'),
);
function displayform()
{
echo '
';
}
function searchresults($resultsarray,$word,$originalarray)
{
print "Searched phrase: $word
";
print "Search Results: (".(count($resultsarray)).") pages found.";
print "
Pages:
";
// display pages
for ($counter=0;$counter<(count($resultsarray));$counter++)
{
$temp = $resultsarray[$counter];
print "
".$originalarray[$temp][1]."Description: ".$originalarray[$temp][2]."
";
}
}
function search($whicharray,$word)
{
$numberarray = array();
$tempcount = 0;
foreach ($whicharray as $number => $data)
{
//print $number;
foreach ($data as $value)
{
//print $value;
if ((trim(strtolower($value)) == trim(strtolower($word))) && ($tempcount > 2))
{
array_push($numberarray, $number);
}
$tempcount ++;
}
$tempcount=0;
}
return $numberarray;
}
if (empty($stage)) $stage = 1;
switch ($stage)
{
case 1:
displayform();
break;
case 2:
$results = (search($searcharray,$searchstring));
//print $results[0];
//print $searcharray[$results][0];
if (!empty($results))
{
searchresults($results,$searchstring,$searcharray);
}
else print "No results found for ".$searchstring;
}
?> [/PHP]
goto [url]http://www.exzibit.co.za/search.php[/url]
to view the script in action.
Search for 'home' and 'products'. This script can return more than 1 result!"
edwin said: "wow! that's excellent. that could be helpful for a lot of site.
good work."
Darksat said: "Wow thats a good script, sleek simple and well written.
I like it."
<h1> said: "sweet thanx alot :D"
Darksat said: "No Problem, your good with PHP, you deserve recognition."