Often we use arrays but most of the time we forget about the keys in an Array().
If you are the programmer who uses only for() loop function then you are going to learn one more loop function called foreach(). When I first start learning PHP I used to loop through array() only with for() loop, but you know after I start using foreach() I almost forgot using the other loop methods.
So, basically if you want to print out both keys and values you better use foreach() function over for() loop function.
This example shows how to echo both the keys and values from an Array();
1 2 3 4 5 6 7 8 9 |
<?php $myarray = array("one"=>"Apple", "two"=>"Ball", "three"=>"Cat"); foreach($myarray as $key=>$val){ echo $key."-".$val."<br>"; } ?> |
I hope this would have helped you, keep learning and keep doing programming. Enjoy!