【问题标题】:i want to check if a word not exists in a array php [duplicate]我想检查数组php中是否不存在一个单词[重复]
【发布时间】:2015-09-09 17:45:00
【问题描述】:

我想检查某个单词是否已经存在于数组中,例如'kijken'。

这个词在数组中不存在,我想添加它。

如何检查单词是否存在?

我的数组如下所示:

Array
(
    [0] => vegen
    [1] => veeg
)
Array
(
    [0] => staan
    [1] => sta
)

【问题讨论】:

  • SO 不是免费的编码资源如果你告诉我如何中奖,我会告诉你如何编码>
  • 我确实尝试过使用 in_array 但无法编写 goog 代码来完成这项工作
  • @RiggsFolly 那里有骗子Smokey ;-)(关门去吃晚饭)

标签: php arrays


【解决方案1】:

已经很经典的功能:

function in_array_r($needle, $haystack, $strict = false) {
  foreach ($haystack as $item) {
    if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
        return true;
    }
}

return false;
}

$term = 'kijken';

if ( in_array_r($term,$yourarray) == true ) {
     //do stuff
} else {
    echo $term.' not in array';
}

【讨论】:

  • 感谢此代码,如果它对我有用,我会尝试
  • 不能保证我们会在答案旁边看到一个绿色勾号,但让我们祈祷吧 ;-)(鉴于他们的记录)。我猜他们不知道 Stack 是如何滚动的。
  • 没关系。很高兴有帮助。我记得曾几何时处于这个位置。 :)
  • @PeterNoble 毫无疑问,彼得和你相当高尚(编辑:没有双关语)。然而,很多时候 OP 会说“它不起作用....” Stack 上的人看到了多少次;无数。让我们只希望它不会为你打开一罐蠕虫;-)但我会投票。
  • 干杯。碰巧,我有几个小时的空闲时间,所以没有伤害。 :)
猜你喜欢
  • 2015-11-17
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 2021-04-04
  • 2023-01-23
相关资源
最近更新 更多