Array Search in php

I searched php.net for array search function but I did n’t find any so I thought about writing my own function for it

Here is a preview of the function:

Suppose if there is an array of names -John,Charles,Richard,Richard and Bill.

You want to find how many times Richard is in the array and in which array placethe name”Richard” is…(Richard is displayed 2 times in the array and it is 2,3)

Here is the array function which will help you to find these attributes

<?

function arraysearch($search,$array)
{
$cc=count($array);
for($i=0;$i<$cc;$i++)
{
$d1=$array[$i];
if($d1==$search)
{
$array1=($d1.”,”.$array1);
$array2=($i.”,”.$array2);
}
}
echo”<pre>”;
/*echo $array1;
echo”<BR>”;
echo $array2;
echo”<BR>”;
*/
$array1=explode(”,”,$array1);
$c1=count($array1);
//print_r($array1);
unset($array1[$c1-1]);
$array2=explode(”,”,$array2);
//print_r($array2);
$c2=count($array2);

unset($array2[$c2-1]);
$c2=count($array2);
//echo $c1;
if(!search)
{

}
else
{

echo “No of $search is $c2″;
}

//return($array1);
echo”<BR>”;
$array2=implode(”,”,$array2);
return($array2);
}
$q = stripslashes ($HTTP_GET_VARS['q']);
$c = stripslashes ($HTTP_GET_VARS['c']);
$c=explode(”,”,$c);

$a1= arraysearch($q,$c);
//print_r($a1);
echo”</pre>”;
?>

This entry was posted on Monday, June 9th, 2008 at 12:17 am and is filed under Php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post a Comment