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

时间:2016-09-29 11:05:47

标签: php arrays json laravel-blade laravel-4.2

我的刀片模板中有条件,检查客户是否从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我在数组[data] [incoming]中循环搜索地址strpos($date['address'], $address['address']) !== false然后当找到地址时我想要通过条件。

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

1 个答案:

答案 0 :(得分:1)

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

@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&gt; = 5.5.0,您可以访问数组列函数,请查看https://stackoverflow.com/a/34785508/2180572以获取解释和示例。在你的代码中用...替换php块

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