2

正在处理一些评论提要。有两个数组 arrayone 带有注释,arraytwo 带有自定义值 如果 arraytwo 的值存在于 arrayone 中,则应该从 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

4 回答 4

1

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

它是基于 pcre 的正则表达式扩展的一部分,称为preg_grep.

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

$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 已被过滤掉。

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

另见:

于 2013-08-17T07:36:47.603 回答
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);
于 2013-08-17T07:04:46.207 回答
0

Simple foreach

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

var_dump($array);
于 2013-08-17T07:07:35.750 回答
0

使用预定义功能非常简单......

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

输出:

数组( [1] => 蓝色)

    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
于 2014-08-13T06:51:03.147 回答