how to remove array2 from array1?

Discussion in 'PHP' started by falcondriver, Nov 11, 2006.

  1. #1
    hi there,

    i have an array with a splited product name, like

    Mac
    Allen
    15
    years
    old

    in array1. array 2 may contain some "general" words like

    old
    year
    years

    so is there any smart function to remove all words in array2 from array 1, so that array1 has only "Mac Allan 15" left?
     
    falcondriver, Nov 11, 2006 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can use
    
    <?php 
    
    $arr1 = array("one", "two", "three");
    $arr2 = array("one", "three");
    foreach ($arr2 as $value)
    {
    unset($arr1[array_search($value,$arr1)]);
    }
    
    ?>
    
    
    Code (markup):
     
    maiahost, Nov 11, 2006 IP
    falcondriver likes this.
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    nico_swd, Nov 11, 2006 IP
    falcondriver likes this.
  4. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #4
    ah great, i already came along this array_diff functions before i made this posting, but somehow i overlooked this one :)
     
    falcondriver, Nov 11, 2006 IP