Php if condition inside foreach

血红的双手。 提交于 2019-12-25 19:58:32

问题


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.


回答1:


Is this what you mean?

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



回答2:


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>";
      }


来源:https://stackoverflow.com/questions/30296776/php-if-condition-inside-foreach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!