php - 如何检查项目数组是否仅包含此项?

标签 php arrays laravel

$items1 = ['apple', 'tree', 'juice'];
$items2 = ['apple', 'tree'];
$items3 = ['apple'];

// loop
If ($items[i].containsOnly['apple'])
{
  // do something..
}

在上面的简化示例中,我想获取与给定项目匹配的数组。是否有类似于“containsOnly”的可用方法?或者最好的方法是什么?

最佳答案

//If the array has the only item present    
   if(in_array('apple',$item) && count($item)==1)
    {
    //Do Something
    }

关于php - 如何检查项目数组是否仅包含此项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55218373/

相关文章:

arrays - Mongoose 模式需要可以为空的数组

php - Laravel - Eloquent - 暂时禁用 touchOwners

php - 请求 - 从 php 服务器到 ruby​​ on rails 服务器的响应。 CSRF token 问题

javascript - hasChildNodes is not a function 小老鼠编辑器js错误

c++ - 检查二维井字数组位置 C++

JavaScript 数组 : How to check if two consecutive words are the same?

PHP fatal error :Uncaught Error: Failed opening required 'new-project/vendor/autoload.php' (include_path ='.;C:\php\pear' ) in E:\new-project\artisan:18

laravel - 只需在 laravel 中验证电子邮件而无需创建验证器

php - PDO 微软访问

php - 查找二叉树级别是否已完成的最佳且有效的方法应该是什么?