PHP只为每个输出一个值 [英] PHP output only one value in for each

查看:95
本文介绍了PHP只为每个输出一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中的每个函数输出都有问题(实际上不知道如何设置代码来做我需要的)。如果foreach中的每个项目都等于某个值,我想输出一些文本。如果我把

I'm having trouble with output in for each function in PHP (actually don't know how to set the code to do what I need). I'd like to output some text if every item in foreach is equal to some value. If I put

foreach($items as $item) {
    if($item == 0){ echo "true"; }
}

我会为每个项目都生效,我只需要输出true所有项目都等于某个值。

I will get true for every item and I need to output true only if all items are equal to some value.

谢谢!

推荐答案

这段代码适用于大多数类型的变量。了解内联评论的工作原理。

This peice of code work for most type of variables. See how it works in inline comment.

 $count=0; // variable to count the matched words
 foreach ($items as $item)
   {
     if($item == $somevalue)
       {
         $count++; // if any item match, count is plus by 1
       }
   }
 if($count == count($items))
   {
     echo "true"; // if numbers of matched words are equal to the number of items
   }
 else
  {
    echo "false";
  }

希望它有效,对不起任何错误

Hope it works , And sorry for any mistake

这篇关于PHP只为每个输出一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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