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.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Manas
  • 3,060
  • 4
  • 27
  • 55
  • `@` is for [error reporting](http://php.net/manual/en/language.operators.errorcontrol.php). `$k` is the [key](http://php.net/manual/en/control-structures.foreach.php) of an associative array. Have you tried the [manual](http://php.net/manual/en/index.php) or done any searching prior to submitting your question? – Pyromonk May 19 '17 at 02: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? – Manas May 19 '17 at 03:02
  • if you use foreach, you don't need to call `$insuranceType_list[$k]`, try to change it to `$l` instead – Ukasha May 19 '17 at 03: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. – Pyromonk May 19 '17 at 03: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. – Pyromonk May 19 '17 at 03:11
  • As for the `->`, [this topic](http://stackoverflow.com/questions/3037526/where-do-we-use-the-object-operator-in-php) might help. – Pyromonk May 19 '17 at 03:14
  • @pyromonk so let's say '$students = array('1' => (name,age,height), '2' =>('mike,20,180))' and 'foreach $students as $k=> $student' and if i do '$students[$k] -> grade = 5. this is gonna add grade to each of the students right? – Manas May 19 '17 at 03:23
  • @Mikethetechy, no, this is not valid php. Please refer to [CodeGodie](http://stackoverflow.com/users/2649661/codegodie)'s answer for more information on your problem in particular and to [this topic](http://stackoverflow.com/questions/16629371/php-retrieving-array-values-using-dash-arrow) for more information on the `->` operator. – Pyromonk May 19 '17 at 03:29
  • thx @Ukasyah for your help – Manas May 19 '17 at 03:30
  • My pleasure. Besides, I've learned a lot from @Pyromonk :) – Ukasha May 19 '17 at 03:32

1 Answers1

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);
    }
}
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • 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? – Manas May 19 '17 at 03:28
  • 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. – CodeGodie May 19 '17 at 03:30
  • 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 !! – Manas May 19 '17 at 03:34
  • 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. – CodeGodie May 19 '17 at 03:35
  • ohhh ic! i will take note of that :D. thx bro! happy coding! cheers. – Manas May 19 '17 at 03:37
  • yup, bottoms up. Cheers. – CodeGodie May 19 '17 at 03:38