$value){echo $key."
";}"."/>
$value){echo $key."
";}".">
Home >Backend Development >PHP Problem >How to print only array subscript in php
In PHP, you can use the foreach and echo statements to loop through the array and print the array subscript, the syntax is "foreach($array as $key => $value){echo $key."
";}".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
How to print only the array subscript in php
Implementation:
Use the foreach statement to traverse the array;
In the loop, use the echo statement to print Array subscript.
Implementation code:
<?php header("Content-type:text/html;charset=utf-8"); $array= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); var_dump($array); foreach($array as $key => $value){ echo $key."<br>"; } ?>
Description:
foreach is specially designed for traversal Statements designed for arrays are a commonly used method when traversing arrays, and provide great convenience in traversing arrays; after PHP5, objects can also be traversed (foreach can only be applied to arrays and objects).
The foreach statement has two syntax formats:
Syntax format 1:
foreach ($array as $value){ 语句块; }
Traverse the given $array array and assign the value of the current array in each loop Give $value.
Syntax format 2:
foreach ($array as $key => $value){ 语句块; }
Traverse the given $array array, and in each loop, the value of the current array will be assigned to $value, and the key name will be assigned to $key.
Foreach statement, each time it loops, the pointer inside the array will move forward one step, so that the next array element will be obtained in the next loop, until the end of the array is traversed, the traversal stops and exits cycle.
Recommended learning: "PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of How to print only array subscript in php. For more information, please follow other related articles on the PHP Chinese website!