Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2004, 08:37 PM   PM User | #1
trib4lmaniac
Regular Coder

 
trib4lmaniac's Avatar
 
Join Date: Feb 2004
Location: Cornwall, UK
Posts: 535
Thanks: 0
Thanked 0 Times in 0 Posts
trib4lmaniac is an unknown quantity at this point
Removing a variable from an array

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..
trib4lmaniac is offline   Reply With Quote
Old 04-14-2004, 08:48 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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;
    }

__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-14-2004, 08:51 PM   PM User | #3
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
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);
?>
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:
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]);
     } 
   } 
 } 
I think that works anyway...

Hope that helps,
Sadiq.

Last edited by sad69; 04-14-2004 at 08:56 PM..
sad69 is offline   Reply With Quote
Old 04-14-2004, 08:53 PM   PM User | #4
trib4lmaniac
Regular Coder

 
trib4lmaniac's Avatar
 
Join Date: Feb 2004
Location: Cornwall, UK
Posts: 535
Thanks: 0
Thanked 0 Times in 0 Posts
trib4lmaniac is an unknown quantity at this point
Great 2 replys in 14mins. Thanks I'll brb after I've tried them...
trib4lmaniac is offline   Reply With Quote
Old 04-14-2004, 08:55 PM   PM User | #5
trib4lmaniac
Regular Coder

 
trib4lmaniac's Avatar
 
Join Date: Feb 2004
Location: Cornwall, UK
Posts: 535
Thanks: 0
Thanked 0 Times in 0 Posts
trib4lmaniac is an unknown quantity at this point
Duh, stupid me raf's answers so simple! Why did't I think of that?
trib4lmaniac is offline   Reply With Quote
Old 04-14-2004, 08:58 PM   PM User | #6
trib4lmaniac
Regular Coder

 
trib4lmaniac's Avatar
 
Join Date: Feb 2004
Location: Cornwall, UK
Posts: 535
Thanks: 0
Thanked 0 Times in 0 Posts
trib4lmaniac is an unknown quantity at this point
Sadiq's answer ain't to tricky either, thanks guys

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

Last edited by trib4lmaniac; 04-14-2004 at 09:01 PM..
trib4lmaniac is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:51 PM.


Advertisement
Log in to turn off these ads.