How would you remove a variable from an array and them budge any subsequent values 'down the line'. I need these for a rookie search engine,
i.e. Google's 'can is a very common word and was not included in your search' thing.
PHP Code:
$common=array('the', 'is', 'a', 'i'); // Would be a lot more words here
$kws=explode(' ', strtolower($_GET['q'])); // Users search broken down into keywords
for($i=0;$i<count($kws);$i++)
{
for($j=0;$j<count($common);$j++)
{
if($kws[$i]==$common[$j])
{
/*
Do something to remove $kws[$i] then
push anything after down into previous' slot
*/
}
}
}
So if the user searched for 'is the grass green' after the script $kws would equal array('grass', 'green').
Thanks
Last edited by trib4lmaniac; 04-14-2004 at 08:42 PM..
This will build a new array with all words that are not inside the $common array
PHP Code:
$common=array('the', 'is', 'a', 'i'); // Would be a lot more words here
$kws=explode(' ', strtolower($_GET['q'])); // Users search broken down into keywords
$keywords = array();
foreach($kws as $var){
if (!in_array($var, $common)){
$keywords[]=$var;
}
}
I think this is what you're looking for (found it in the user comments at http://ca2.php.net/array)
Quote:
unset($bar['mushroomsoup']) only works it the key
is 'mushroomsoup'.If you want to erase elements
of an array identified by values rather than by keys
you can use this function:
PHP Code:
<?
function unset_by_val($needle,&$haystack) {
# removes all entries in array $haystack,
# who's value is $needle
while(($gotcha = array_search($needle,$haystack)) > -1)
unset($haystack[$gotcha]);
}
$ring = array('gollum','smeagol','gollum','gandalf',
'deagol','gandalf');
print_r($ring); echo "<br>";
unset_by_val('gollum',$ring);
print_r($ring);
?>
And then there are 2 ways to apply it to your code:
1:
PHP Code:
$common=array('the', 'is', 'a', 'i'); // Would be a lot more words here
$kws=explode(' ', strtolower($_GET['q'])); // Users search broken down into keywords
for($i=0;$i<count($kws);$i++)
{
for($j=0;$j<count($common);$j++)
{
unset_by_val($common[$i], $kws);
}
}
2:
PHP Code:
$common=array('the', 'is', 'a', 'i'); // Would be a lot more words here
$kws=explode(' ', strtolower($_GET['q'])); // Users search broken down into keywords
for($i=0;$i<count($kws);$i++)
{
for($j=0;$j<count($common);$j++)
{
if($kws[$i]==$common[$j])
{
unset(kws[$i]);
}
}
}