Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    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.

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great 2 replys in 14mins. Thanks I'll brb after I've tried them...

  • #5
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Duh, stupid me raf's answers so simple! Why did't I think of that?

  • #6
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •