简体   繁体   English

PHP if 条件在 foreach

[英]Php if condition inside foreach

I have following arrays.我有以下数组。

Array
(
    [0] => Array
        (
            [0] => Cash
            [1] => 91.16
        )
    [1] => Array
        (
            [0] =>Credit
            [1] => 61.48
        )
)

I want to do something like this .我想做这样的事情。

foreach ($value as $values) {
// Where $values is the above array . I want to traverse array dynamically and put if condition .
if($values[0][0] != "Cash")
echo "Cash";
}

The above code does not working .上面的代码不起作用。 Please help me on this .请帮我解决这个问题。 How can I place if condition dynamically inside foreach loop.如何在 foreach 循环中动态放置 if 条件。

Is this what you mean?你是这个意思吗?

foreach ($values as $value) {
  if (!in_array($value[0], array('Cash', 'Credit'))) {
    echo 'Neither cash nor credit';
  }
}

try like this像这样尝试

    foreach ($array as $values) {
          if($values[0] != "cash" && $values[0] == "credit"){
           echo "It is a credit ";
          }
   else if($values[1] != "credit" && $values[0] == "cash"){
      echo "it is a cash"."<br>";
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM