I'm trying to use the foreach loop, but I'm not sure how to do it and I didn't understand it from the link either.
<?php // this array has 3 elements: one, two and three $myArray = array('one', 'two', 'three'); // this will 'iterate' (repeat over) each element in the array foreach ($myArray as $element) { // so this is executed 3 times echo $element; } ?> PHP:
I pretty much understand that, so I could replace echo with my function that I made and it would do that for each element of my array? Also, could element have been any name since it appears to be a variable?
Can I iterate through two arrays at the same time at the same speed? Or can I add to a variable inside the function every time it's iterated? Either one of these will work for me.
As Deacalion said, the best approach is array_walk: $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); function test_print($item, $key) { echo "$key. $item<br />\n"; } array_walk($fruits, 'test_print'); PHP: You can see that array_talk will call the function test_print for every item in the array.
Can someone explain that chunck of code? I don't understand what it does and does it do what I said I wanted it too?