Laravel foreach view

Asked

Viewed 4,423 times

-5

  • An infinite array? Imagine how much processing you won’t need.

  • I already have it old, I say infinite, because in fact I’ll never know qnd it ends, it can end one or levels, or with up to x levels

1 answer

5


An example of how to iterate an array, suppose you have the following array:

array (size=3)
  'clients' => 
    array (size=4)
      'Super Fish' => 
        array (size=2)
          'totalminutes' => int 49
          'totalcost' => float 28.58774
      'Swedish Fish' => 
        array (size=2)
          'totalminutes' => int 16
          'totalcost' => float 5.85842
      'Aero Fish' => 
        array (size=2)
          'totalminutes' => int 7
          'totalcost' => float 0.29714
      'Happy Fish' => 
        array (size=2)
          'totalminutes' => int 44
          'totalcost' => float 18.16224
  'totalminutes' => int 116
  'totalcost' => float 52.90554

your controller would look like this code:

$data = Reports\InternationalCallsReport::create();
return View::make('reports.international-calls')->with('data', $data);

the code in the view only with foreach:

foreach($data['clients'] as $k => $v) {
    echo $k . '<br>';
    foreach ($v as $key => $value) {
        echo $key . ' => ' . $value . '<br>';
    }
}

In the Blade version:

@foreach($data['clients'] as $k => $v)
    {{ $k }} 
    @foreach ($v as $key => $value) 
        {{ $key . ' => ' . $value }}
    @endforeach
@endforeach

No matter if it is finite or infinite, the [FOREACH][1] will go through all the elements until the array ends.

But this is good?

It depends on what you are trying to do, anyway there are several ways to an infinite loop. As for example:

you can create a condition with the if .. if the time takes longer than 3 minutes and then use the code [break][1], break ends the execution of the current for, foreach, while, do-while or switch structure.

some reference links that may help:

Laravel Foreach with Bank
Pass Array to Blade

  • I know the question is not very clear, but if my answer helped, please study voting for it as useful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.