拆卸一个数组的元素,如果它是在另一个发现 [英] Remove elements of one array if it is found in another

查看:110
本文介绍了拆卸一个数组的元素,如果它是在另一个发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1728727/remove-item-from-array-if-it-exists-in-a-disallowed-words-array\">Remove如果它存在于一个不允许的词阵列从阵列项

我有一个动态的字符串,客户端将发送和我要创建以逗号分隔的标签从它:

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

如果的 $ definite_article 数组中的这些词汇之一是在 $标记阵列删除键和值这个词和新的阵列将已经采取了这些话。我都会有这样的阵列由 array_rand 用来选择出来的词的随机组。我试过很多东西来实现我的结果,但没有那么远。有人能帮我找到一个解决这个?

If one of these words in the $definite_article array are in the $tags array 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_rand to 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?

推荐答案

您正在寻找 和array_diff

You are looking for 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.

这篇关于拆卸一个数组的元素,如果它是在另一个发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆