I have this array and it can be infinite with several children link How do I make a foreach in this in a view?
I have this array and it can be infinite with several children link How do I make a foreach in this in a view?
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:
$data = Reports\InternationalCallsReport::create();
return View::make('reports.international-calls')->with('data', $data);
The code in the view with just 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
It does not matter if it is finite or infinite, [FOREACH][1]
will scroll through all the elements until the array is finished.
But is this good?
It depends on what you are trying to do, however there are several ways to for an infinite loop. For example:
You can create a condition with
if
.. if the time takes longer than 3 minutes and then use the code[break][1]
, the break ends the execution of the for, foreach, while, do-while, or current switch structure.
Some reference links that may help: