...

View Full Version : Removing a variable from an array



trib4lmaniac
04-14-2004, 09:37 PM
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.


$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 :thumbsup:

raf
04-14-2004, 09:48 PM
This will build a new array with all words that are not inside the $common array


$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;
}
}

sad69
04-14-2004, 09:51 PM
I think this is what you're looking for (found it in the user comments at http://ca2.php.net/array)



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:


<?
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);
?>

Will output:
Array ( [0] => gollum [1] => smeagol [2] => gollum
[3] => gandalf [4] => deagol [5] => gandalf )
Array ( [1] => smeagol [3] => gandalf
[4] => deagol [5] => gandalf )


And then there are 2 ways to apply it to your code:
1:


$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:


$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]);
}
}
}


I think that works anyway...

Hope that helps,
Sadiq.

trib4lmaniac
04-14-2004, 09:53 PM
Great 2 replys in 14mins. Thanks I'll brb after I've tried them...

trib4lmaniac
04-14-2004, 09:55 PM
Duh, stupid me raf's answers so simple! Why did't I think of that?

trib4lmaniac
04-14-2004, 09:58 PM
Sadiq's answer ain't to tricky either, thanks guys :thumbsup:

btw just using unset($kws[$i]); doesnt work for some reason.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum