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);
}
}
@is for error reporting.$kis the key of an associative array. Have you tried the manual or done any searching prior to submitting your question?$insuranceType_list[$k], try to change it to$linstead$insuranceType_listis an associative array,foreachgoes through each of its keys ($k) and assigns the value at said key to a temporary variable$l. So if you have$insuranceType_list = array('foo' => 'bar', 'bar' => 'foo');, theforeachloop will go through 2 iterations ($kequal to 'foo' and$lequal to 'bar' in the first and$kequal to 'bar' and$lequal to 'foo' in the second). Please refer to the manual entry onforeachfor further examples.