php 如果在另一个数组中找到一个数组的元素,则删除它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10589921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove elements of one array if it is found in another
提问by Tower
Possible Duplicate:
Remove item from array if it exists in a 'disallowed words' array
I have a dynamic string that clients will send and I want to create comma delimited tags from it:
我有一个客户端将发送的动态字符串,我想从中创建逗号分隔的标签:
$subject = "Warmly little in before cousin as sussex and an entire set Blessing it ladyship.";
print_r($tags = explode(" ", strtolower($subject)));
And yet, I want to delete a specific group of words (such as definite articles), but I want to delete the key and value of that word if it is in the exploded array:
然而,我想删除一组特定的单词(例如定冠词),但我想删除该单词的键和值,如果它在分解数组中:
$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');
If one of these words in the $definite_articlearray are in the $tagsarray delete the key and value of that word and the new array will have these words taken out. I will have this array be used by array_randto have a random group of words chosen out of it. I've tried many things to achieve my result, but nothing so far. Can someone help me find a resolve to this?
如果$definite_article数组中的这些单词之一在数组中,则$tags删除该单词的键和值,新数组将删除这些单词。我将使用这个数组来array_rand随机选择一组单词。我已经尝试了很多事情来实现我的结果,但到目前为止还没有。有人可以帮我解决这个问题吗?
回答by Jon
You are looking for array_diff:
您正在寻找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);
回答by Madara's Ghost
Sounds like an easy job for array_diff().
听起来对array_diff().
array array_diff ( array $array1 , array $array2 [, array $... ] )Compares
array1againstarray2and returns the difference.
array array_diff ( array $array1 , array $array2 [, array $... ] )比较
array1反对array2和收益之差。
Which basically means it will return array1after it's been stripped of all values which exist in array2.
这基本上意味着它会array1在删除array2.

