2

am working with on some comments feed. there are two arrays arrayone is with comments and arraytwo is with self defined values if the value of arraytwo is present in arrayone then that value should be removed from arrayone

those are here

    $arraytwo  = array("cool","lo","love");

   $arrayone = Array
    (
        [0] => Aman Namdev Twinkle::how many coast  ,
        [1] => Remi George::cool  ,
        [2] => Swaraj Kumar::wow mind blowing ,
        [3] => Aman Nagar::nice ,
        [4] => Aarya Kumar::H  ,
        [5] => Akash Shah::superb ,
        [6] => Varadraj Nayak::in my city i will get it at 450 D i hav D D ,
        [7] => Ronak Prajapati::prize koti che 400 rs ma male bro ,
        [8] => Praveen Bahukhandi::dikhne me kam nahi kho jayein to gam nahi D ,
        [9] => Keimah Prince Pachuau::it s my type i have three pieces ) ,
        [10] => Chetan Singh::niceeeee  ,
        [11] => Abdus Salaam::Best 1 ,
        [12] => Jagan Nath::Wow nice  ,
        [13] => Tanmay Sankhe::1no banntta  ,
        [14] => Prashant Bhesaniya::Good  ,
        [15] => Yogender Tanwar::nycc waferers ,
        [16] => Ophil Goyal::fuck off 1 months bhi ni chalen gi ,
        [17] => Tarun Khatri::how many coast  ,
        [18] => Jassu Jaz::sexy ,
        [19] => Suraj Khanna::cool  ,
        [20] => Aap Kis "gaon" Se Hain::https www facebook com pages Aap Kis gaon Se Hain 522063624503084 ref hl like dis page ) ,
        [21] => Amit Kumar::https www facebook com LearnAdvanceGeneralKnowledge ref stream&hc location stream ,
        [22] => Sayan Ghosh::awesome  ,
        [23] => Salman Akhtar::n p ,
        [24] => Ashish Kumar::pareshan mt ho le lungaaa ,
        [25] => Vishesh Maheshwari::really like it where can be found it  ,
        [26] => Girjesh Pratap Singh-ll::https www facebook com pages We Can Change It If You Are LOve Our Country Then Join US 192400627596130 ,
        [27] => Hiren Prajapati::bhai tara mate 6  ,
        [28] => Vengatesh Kumar::really cool  ,
        [29] => Pabitra Kumar Samal::dont choice ,
        [30] => Mithlesh Oraon::Bhai ye chasma mujhe doge ,
        [31] => Priya Brata Tripathy::hiiiiiiiiiiiii lishaaaaaaaaaaa ,
        [32] => Ravi Ram::How much costs ,
        [33] => Manjunath Bullet::plz call me i wan t this glass my mun 9743001183 ,
        [34] => Sizziling Angel::i have dis glairs ,
        [35] => Nagarathinam Mohan::glasssss ,
        [36] => A.k. Meher::i dont like after one month dmage this sunglas i am use this sunglas ,
        [37] => Ram PArkash Goyal::I love u I love u I love u I wanna spend my life with only You ,
        [38] => Deepak Kumar::its mine ,
        [39] => Sanjay Pareek Monu::rkhe hui acche nhe lgthe lga le  ,
        [40] => Jyothi Budupula::like this ,

    )



   foreach($arraytwo as $sa)
        {
    foreach($arrayone as $fa)
    {

    $result1 = str_replace($sa,"",$fa);

        }

    }
3
  • show your code what you have tried. Commented Aug 17, 2013 at 6:55
  • Do you want us to start from scratch? Expose your code. Commented Aug 17, 2013 at 7:02
  • foreach($arraytwo as $sa) { foreach($arrayone as $fa) { $result1 = str_replace($sa,"",$fa); } } Commented Aug 17, 2013 at 7:04

4 Answers 4

1

PHP ships with a function that is pretty well for looking into each array entry for string values and then filtering (or searching) based on a match.

It's part of the pcre based regex extension and called preg_grep.

In your case you pick your $arryTwo input and turn it into a regex pattern. Then this pattern is used to grep the $arrayOne by inverting the matches (that means your matches are removed).

$pattern  = sprintf('~(\\Q%s\\E)~', implode('\\E|\\Q', $arrayTwo));
$filtered = preg_grep($pattern, $arrayOne, PREG_GREP_INVERT);

The result then is a filtered array (demo):

Array
(
    [0] => Aman Namdev Twinkle::how many coast  
    [3] => Aman Nagar::nice 
    [4] => Aarya Kumar::H  
    [5] => Akash Shah::superb 
    ...

As this example output shows here, the entries 1 and 2 are filtered out.

You can use the same function in case you want to offer a search over the chat. Regular expressions are pretty powerful when it comes to strings however they are not always straight forward so take your time to learn about them if you need to deal with string searches more often because then it's valuable for you.

See as well:

Sign up to request clarification or add additional context in comments.

Comments

0

Simple foreach

foreach ($arrayone as $key => $value)
{
    foreach ($arraytwo as $keyword)
    {
        if(preg_match('/'.$keyword.'/', $value))
            unset($array[$key]);
    }
}

var_dump($array);

1 Comment

Imagine on the same entry multiple keywords are matching.
0

Try this.

$temp=array();
$arrayone= array([0]=>..,. ......, [40]=>...);
foreach($arrayone as $key=>$val) {
   for($i=0;$i<count($arraytwo);$i++) {
      $pos = strpos($val, $arraytwo[$i]);
      if($pos === true) {
        $temp[$key] = $val;
      }
   }
}
$result = array_diff($arrayone,$temp);
print_r($result);

Comments

0

Its very simple to use predefine function.....

<?php $array1 = array("a" => "green", "red", "blue", "red"); 
      $array2 = array("b" => "green", "yellow", "red"); 
      $result = array_diff($array1, $array2);
      print_r($result);
?>

output:

Array ( [1] => blue )

    I needed to remove the contents of $array1 from $array2 so I tried:

   <?php 
        $diff    = array_diff($members1, $members2);
   ?> 
   WRONG!! A quick swap around and things worked smoothly...
   <?php  
       $diff    = array_diff($members2, $members1); 
  ?> 
  Hope this saves someone a bit of bother

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.