Garg Garg - 2 months ago 7
JSON Question

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

I have condition in my blade template which check if customer is paid from api call. The problem is that doesn't matter if some of

if/elseif
conditions is met it's always goes to
else
and show what is in there.

@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


I've made it like this because if I put inner if/else block inside foreach I've got even unwanted results on the page because they are multiple elements inside
$totala['data']['vouts']
and foreach loop iterate until there is no more elements..

So why the result always goes in else block?

Note: All data is there and is correct. I've triple checked the data in the json array.

Update: Here is example of the array

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

)
)
)
)


With
foreach
I'm looping in array[data][incoming] to search for the address
strpos($date['address'], $address['address']) !== false
then when the address is found I want to go trough conditions.

I just realized that I's always goes in else block because they are multiple arrays in [incoming] and there always conditions are not met. Is that correct? If is that how can I fix it?

Answer

Messy, but I think this will achieve what you need.

@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

If you are only interested in the matching data you should extract this information in your controller - not in the view.

Update:
If using PHP >= 5.5.0 you have access to the array column function, have a look at http://stackoverflow.com/a/34785508/2180572 for an explanation and example. In your code replace the php block with...

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