可能重复:
Remove item from array if it exists in a 'disallowed words' array
我有一个客户端将发送的动态字符串,我想从中创建逗号分隔的标签:
$subject = "Warmly little in before cousin as sussex and an entire set Blessing it ladyship.";
print_r($tags = explode(" ", strtolower($subject)));
然而,我想删除一组特定的单词(例如明确的文章),但我想删除该单词的键和值(如果它在爆炸数组中):
$definite_articles = array('the','this','then','there','from','for','to','as','and','or','is','was','be','can','could','would','isn\'t','wasn\'t', 'until','should','give','has','have','are','some','it','in','if','so','of','on','at','an','who','what','when','where','why','we','been','maybe','further');
如果$definite_article
数组中的其中一个单词位于$tags
数组中,则删除该单词的键和值,新数组将取出这些单词。我将array_rand
使用此数组来从中选择一组随机单词。我已经尝试了很多东西来实现我的结果,但到目前为止还没有。有人可以帮我找到解决方法吗?
答案 0 :(得分:70)
您正在寻找array_diff
:
$subject = "Warmly little in before cousin as sussex...";
$tags = explode(" ", strtolower($subject));
$definite_articles = array('the','this','then','there','from','for','to','as');
$tags = array_diff($tags, $definite_articles);
print_r($tags);
<强> See it in action 强>
答案 1 :(得分:33)
array_diff()
听起来很容易。
array array_diff ( array $array1 , array $array2 [, array $... ] )
将
array1
与array2
进行比较并返回差异。
这基本上意味着在array1
中删除array2
中存在的所有值后,它将返回{{1}}。