1

In the below code I understand that foreach $insuranceType_list is assigning the row of data to $l. What is the $k for? For index? And @ in front of any variable could make the variable into a global one? Which means I can access it anywhere throughout my application?

if(@$insuranceType_list){
  foreach($insuranceType_list as $k=>$l){
    $insuranceType_list[$k]->version_list = $this->mdl_quotation_template->getConditionTemplates('insurance_type_id='.$l->id);
  }
}

I also don't get what is going on here:

$insuranceType_list[$k]->version_list 

It would be helpful if someone can explain me.

10
  • @ is for error reporting. $k is the key of an associative array. Have you tried the manual or done any searching prior to submitting your question? Commented May 19, 2017 at 2:57
  • @Pyromonk sorry i did try to search ..i got confused...is $k is the key of an associative array. like it assigns keys in terms of 1.$l , 2.$l and so on... right? Commented May 19, 2017 at 3:02
  • if you use foreach, you don't need to call $insuranceType_list[$k], try to change it to $l instead Commented May 19, 2017 at 3:05
  • In your example, $insuranceType_list is an associative array, foreach goes 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');, the foreach loop will go through 2 iterations ($k equal to 'foo' and $l equal to 'bar' in the first and $k equal to 'bar' and $l equal to 'foo' in the second). Please refer to the manual entry on foreach for further examples. Commented May 19, 2017 at 3:08
  • @Ukasyah, that is if the variable is unset after the loop gets executed. I would not recommend to go there at this stage. In my opinion, the original poster is safer by using keys and not worrying about this aspect of variable scope before he fully understands loops and arrays. Commented May 19, 2017 at 3:11

1 Answer 1

2

Lets dissect:

  1. $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']
  2. 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.
  3. 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);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

thx a lot for the detailed explanation @CodeGodie. so over all $insuranceType_list[$k]->version_list means that i am adding the version_list to each of my $insuranceType_list right?
You're welcome. And yes, when using objects (-> notation), you can easily add on extra values by assigning them in the iteration as you are doing in $l->version_list = ... meaning if that object had a property called version_list, it will reassign, but if it doesnt exist it will create it. Hope that makes sense.
thxx! that's what got me confused. i have always been using this format foreach ($lists as $k=>$list) and if i want to add anything we could do like you mentioned above $list->version_list. thax a lot !!
No problem. Keep in mind, this will only work with objects. If you had an array of arrays you would need to pass it by reference which is a whole other monster. Its better to work with objects.
ohhh ic! i will take note of that :D. thx bro! happy coding! cheers.
|

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.