在匹配情况下从其他数组中删除数组中的值

时间:2013-08-17 06:47:28

标签: php arrays

正在处理一些评论Feed。 有两个数组arrayone是带注释而arraytwo是自定义值 如果在arrayone中存在arraytwo的值,则应该从arrayone

中删除该值

那些在这里

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

        }

    }

4 个答案:

答案 0 :(得分:1)

PHP提供的函数非常适合查看字符串值的每个数组条目,然后根据匹配进行过滤(或搜索)。

它是基于pcre的正则表达式扩展的一部分,并调用preg_grep

在您的情况下,您选择$arryTwo输入并将其转换为正则表达式模式。然后,通过反转匹配(这意味着您的匹配被删除),此模式用于gre $arrayOne

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

结果是过滤后的数组(demo):

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

如此示例输出所示,条目1和2被过滤掉。

如果您想通过聊天提供搜索,可以使用相同的功能。正则表达式在字符串方面非常强大,但它们并不总是直截了当所以如果你需要更频繁地处理字符串搜索,请花些时间来了解它们,因为它对你很有价值。

参见:

答案 1 :(得分:0)

试试这个。

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

答案 2 :(得分:0)

简单foreach

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

var_dump($array);

答案 3 :(得分:0)

使用预定义函数非常简单.....

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

输出:

阵 ([1] =&gt;蓝色 )

    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