To iterate over an array in PHP, you can use a foreach loop. The syntax for the loop is as follows:
main.php114 chars10 lines
In the above example, we have an array containing 'apple', 'banana', and 'orange'. We use the foreach loop to iterate over each value in the array and print it out. The loop will continue until all the values in the array have been iterated over.
The output of the above code will be:
main.php20 chars4 lines
You can also use the foreach loop to iterate over the keys and values of an associative array. The syntax for this is as follows:
main.php173 chars10 lines
In this example, we have an associative array containing fruits and their colors. We use the foreach loop to iterate over each key-value pair in the array and print out the key and value. The output of the above code will be:
main.php47 chars4 lines
gistlibby LogSnag