我正在处理一些注释提要。有两个数组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);
}
}
PHP附带了一个函数,该函数非常适合查找每个数组条目中的字符串值,然后根据匹配进行过滤(或搜索)。
它是基于pcre的regex扩展的一部分,称为preg_grep
。
在您的情况下,您可以选择$arryTwo
输入并将其转换为正则表达式模式。然后,这个模式用于通过反转匹配来grep $arrayOne
(这意味着您的匹配被删除)。
$pattern = sprintf('~(''Q%s''E)~', implode('''E|''Q', $arrayTwo));
$filtered = preg_grep($pattern, $arrayOne, PREG_GREP_INVERT);
结果是一个经过过滤的阵列(演示):
Array
(
[0] => Aman Namdev Twinkle::how many coast
[3] => Aman Nagar::nice
[4] => Aarya Kumar::H
[5] => Akash Shah::superb
...
如本示例输出所示,条目1和2被过滤掉。
如果你想通过聊天进行搜索,你可以使用相同的功能。正则表达式在字符串方面非常强大,但它们并不总是直截了当的,所以如果你需要更频繁地处理字符串搜索,请慢慢学习,因为这对你来说很有价值。
另请参阅:
- 数组搜索中的通配符(2011年10月)-这个问答;A展示了如何将
preg_gep
与自己的模式一起使用,例如SQLLIKE模式MysqlDocs
简单foreach
foreach ($arrayone as $key => $value)
{
foreach ($arraytwo as $keyword)
{
if(preg_match('/'.$keyword.'/', $value))
unset($array[$key]);
}
}
var_dump($array);
试试这个。
$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);
使用预定义函数非常简单。。。。。
<?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