即使在 if/esleif 块中满足条件,条件也会直接进入 else

Condition goes directly in else even if there is condition which is met in if/esleif block

我的 blade 模板中有条件检查客户是否通过 api 电话付款。问题是,如果满足某些 if/elseif 条件并不重要,它总是转到 else 并显示其中的内容。

@if (is_array($totala['data']['incoming']) || is_object($totala['data']['incoming']))

    @foreach($totala['data']['incoming'] as $key => $date)
    @endforeach
           @if(strpos($date['address'], $address['address']) !== false )

                @if($order['price'] == $date['amount'] )
                    <span>Paid</span>

                @elseif($order['price'] < $date['amount'] )
                    <span>Over Paid</span>


                @elseif($order['price'] > $date['amount'] )
                    <span>Under Paid</span>
                @else                       
                @endif
           @else
                <span>Else block</span>                                
           @endif       
@else
    <span>Unpaid</span>
@endif

我这样做是因为如果我将内部 if/else 块放在 foreach 中,我什至会在页面上得到不需要的结果,因为它们在 $totala['data']['vouts'] 和 foreach 循环迭代中有多个元素直到没有更多的元素..

那么为什么结果总是在 else 块中?

注意:所有数据都在那里并且是正确的。我已经三次检查了 json 数组中的数据。

更新:这里是数组的例子

Array
(
  [status] => success
  [data] => Array
    (
        [id] => id
        [incoming] => Array
            (
                [0] => Array
                    (
                        [address] => address1
                        [amount] => sum1

                    )

                [1] => Array
                    (
                        [address] => address2
                        [amount] => sum2

                    )

                [2] => Array
                    (
                        [address] => address3
                        [amount] => sum3

                    )

                [3] => Array
                    (
                        [address] => address4
                        [amount] => sum4

                    )

                [4] => Array
                    (
                        [address] => address5
                        [amount] => sum5

                    )
            )
    )
 )

使用 foreach 我在 array[data][incoming] 中循环以搜索地址 strpos($date['address'], $address['address']) !== false 然后当找到地址时我想通过条件。

我刚刚意识到我总是进入 else 块,因为它们是 [incoming] 中的多个数组,并且总是不满足条件。那是对的吗?如果是这样我该如何解决?

乱七八糟的,但我认为这将实现你所需要的。

@if (is_array($totala['data']['incoming']) || is_object($totala['data']['incoming']))

   <?php
      // find the first matching address in $totala['data']['incoming']
      $match = false;
      foreach ($totala['data']['incoming'] as $data) {
         if ($data['address'] == $address['address']) {
            $match = $data;
            break;
         }
      }
   ?>

   @if($match !== false )

      @if($order['price'] == $match['amount'] )
         <span>Paid</span>
      @elseif($order['price'] < $match['amount'] )
         <span>Over Paid</span>
      @else
         <span>Under Paid</span>                      
      @endif
   @else
      <span>No match found</span>                                
   @endif
@else
   <span>Unpaid</span>
@endif

如果您只对匹配数据感兴趣,您应该在控制器中提取此信息 - 而不是在视图中。

更新:
如果使用 PHP >= 5.5.0 您可以访问数组列函数,请查看 以获得解释和示例。在您的代码中,将 php 块替换为...

<?php
   $match = array_search($address['address'], array_column($totala['data']['incoming'], 'address'));
   if($match) {
      $match = $totala['data']['incoming'][$match];
   }
?>