Home >Backend Development >PHP Tutorial >How to use foreach statement in php for array elements

How to use foreach statement in php for array elements

不言
不言Original
2018-12-28 13:54:293221browse

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.

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

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

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

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

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more