I have gone over a few different examples but they were limited. I was able to find one piece of code that I would like to disect and ask questions about so I can gain a better understanding.
Expand|Select|Wrap|Line Numbers
- $characters = array
- (
- array ( name=>"name 1"
- , occupation=>"developer"
- , age=>30
- , specialty=>"Java"
- ),
- array
- (
- name=>"name 2"
- , occupation=>"Programmer"
- , age=>24
- , specialty=>"C++"
- ),
- array
- (
- name=>"name 3"
- , occupation=>"designer"
- , age=>63
- , specialty=>"Javascript"
- )
- );
- foreach ($characters as $val)
- {
- foreach ($val as $key=>$final_val)
- {
- print "$key: $final_val<br>";
- }
- print "<br>";
- }
On the foreach, can someone please tell me exactly how and why it is set up the way it is? Specifically, I don't understand why the coder didn't use a key/value pair. He only uses a value, then inside the loop he uses $key=>$final_val. What I need to understand is why and when to refer to or use the key value pair and when not to. I have also seen code written inside the foreach loop like so: $key['something'] = $val;
What is that? What exactly does it do? If any one can help me to understand these, I would be forever grateful. I have pulled my hair out for the last time on drawing my data out of an array.
Thanks.