Lets dissect:
$insuranceType_list
is an array of objects. I know this because you are accessing the values using the arrow as in $l->something
, if you had an array of arrays you would access it like this $l['something']
- the
@
in front just meant that you are suppressing errors to that variable. It is usually used in functions of which you dont want to errors to be thrown.
- you can iterate using a foreach without the
$k
like foreach($insuranceType_list as $l)
which means that you don't care about the index, you just want the value per every iteration. However if you plan on using the key for any reason, then you would use that $k
variable as you are currently doing.
Example:
Lets say you have an array is like this:
$list = [
[
'name' => 'john'
'age' => 29
],
[
'name' => 'jane'
'age' => 23
]
];
if you wanted to create strings that said "John is 29 years old". Then you would do the following:
foreach($list as $l){
echo "{$l['name']} is {$l['age']} years old";
}
however if you wanted the string to say "John is #1 on the list, and is 29 years old". Then you would do the following:
foreach($list as $k => $l){
echo "{$l['name']} is #{$k} on the list, and is {$l['age']} years old";
}
With all that said, i would shorten your code in this manner:
if (!empty($insuranceType_list)) {
foreach ($insuranceType_list as $l) {
$l->version_list = $this->mdl_quotation_template->getConditionTemplates('insurance_type_id=' . $l->id);
}
}