In addition to the for statement and while statement, there is also a foreach statement used to process array loops in How to use foreach statement in How to use foreach statement in How to use foreach statement in php for array elements for array elements for array elements. The foreach statement is a statement that loops array elements. This article will introduce to you the usage of the foreach statement in How to use foreach statement in How to use foreach statement in How to use foreach statement in php for array elements for array elements for array elements.
Let’s first look at the usage of a foreach statement
The foreach statement can be written in the following form
<?How to use foreach statement in How to use foreach statement in How to use foreach statement in php for array elements for array elements for array elements foreach(数组){ 执行处理 } ?>
For example, create a variable called fruit and make it an array.
<?How to use foreach statement in How to use foreach statement in How to use foreach statement in php for array elements for array elements for array elements $fruit = array("香蕉","苹果","橘子"); foreach($fruit as $value){ echo $value . "<br>"; } ?>
The running results are as follows
This is the basic usage of the foreach statement.
We can alsouse the foreach statement in an associative array
<?How to use foreach statement in How to use foreach statement in How to use foreach statement in php for array elements for array elements for array elements $fruit = array("apple" => "苹果" ,"orange" => "橘子","grape" => "葡萄"); foreach($fruit as $key => $value){ echo $value ."用英语写是". $key . "<br>"; } ?>
The running result is as follows
Like this , the foreach statement can be used in associative arrays in the same way.
Summary: This article ends here. For more exciting content, you can pay attention to the relevant column tutorials on the How to use foreach statement in How to use foreach statement in How to use foreach statement in php for array elements for array elements for array elements Chinese website for further study! ! !
The above is the detailed content of How to use foreach statement in php for array elements. For more information, please follow other related articles on the PHP Chinese website!