Home > Backend Development > PHP Problem > How to print only array subscript in php

How to print only array subscript in php

青灯夜游
Release: 2023-03-15 11:08:02
Original
1882 people have browsed it

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."
";}".

How to print only array subscript in php

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>";
}
?>
Copy after login

How to print only array subscript in php

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){
    语句块;
}
Copy after login

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){
    语句块;
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template