0

I have an array of customers that has a nested array of payments.

"customer_1" => array:4 [▼
 0 => "211.79"
 1 => "206.20"
 2 => "0.00"
 3 => "0.00"
 4 => "220.90"
]

 "customer_2" => array:4 [▼
 0 => "0.00"
 1 => "0.00"
 2 => "0.00"
 3 => "0.00"
 4 => "220.90"
]

I need to count, for each customer, the amount of consecutive payments, starting from the top of the array that are 0.00.

So I would need it to return something like:

"customer_1" => 0
"customer_2" => 4

I've tried a bunch of while and foreach loops but can't get it to work:

@php($count = 0)

    @foreach($array as $arr)

        @if($arr = "0.00")
            @php($count = $count + 1)
        @else
            @continue
        @endif

    @endforeach
5
  • Not really clear, according to your example."customer_1" 's value is 211.79+206.20+220.90? Commented Jan 28, 2020 at 5:33
  • I need a count of consecutive "0.00" payments, not a sum of payments. Customer 1 has no consecutive "0.00" payments from the top of the array, therefore customer_1 = 0. Commented Jan 28, 2020 at 5:40
  • But I think customer_1 have two consecutive '0.00',am i wrong? Commented Jan 28, 2020 at 5:46
  • Correct, however not from the start of the array. Their first payment is 211.79 so therefore count should be 0. Commented Jan 28, 2020 at 5:47
  • I have post my answer, plz check it. Commented Jan 28, 2020 at 6:03

3 Answers 3

1

Check the first element, if it is 0.00, then just calculate the consecutive 0.00, or just break the loop:

$count = 0;
if ($array[0] == "0.00") {
    foreach($array as $item) {
        if($item == "0.00") {
            $count += 1;
        } else {
            break;
        }
    }
}
return $count;

For blade:

@php($count = 0)

@if($arr[0] == "0.00")
@foreach($array as $arr)

   @if($arr == "0.00")
      @php($count += 1)
   @else
      @break
   @endif

@endforeach
@endif
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate the blade syntax! Great job.
0

you are assigning value to variable inside if condition , you need to compare "0.00" inside if condition .

@php($count = 0)

  @foreach($array as $arr)

    @if($arr == "0.00")
        @php($count = $count + 1)
    @else
        @continue
    @endif

 @endforeach

2 Comments

Hey still doesn't work, it outputs count as the same amount of payments. customer_1 => 5, customer_2 =>5
@Dexx please look this refernce geeksforgeeks.org/count-consecutive-pairs-of-same-elements hope this is your requirements :)
0
$sum = 0;

foreach($items as $item) {
    $sum += $item;
}

echo $sum;

Try this @dexx

1 Comment

Hey thanks for responding however that is summing the values, I need a count of consecutive "0.00" payments from the beginning of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.