Home >Backend Development >PHP Problem >How to output only array key values in php
Two methods of outputting only key values: 1. Use the "$array variable name [subscript]" statement to access a key value of the specified subscript, and then use the echo or print statement to output the single array key value. Syntax "echo $array variable name[subscript];" or "print $array variable name[subscript];". 2. Use the foreach statement to loop through the array, and then use the echo or print statement to output multiple array key values. The syntax is "foreach($array variable name as $v){echo $v;}".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php only outputs arrays Two methods of key values
Method 1. Output a single array key value
First use "$array variable Name[subscript]" statement accesses an element of the specified subscript
and then uses the echo or print statement to output a single array key value
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(11,null,12,2,3,4,5); var_dump($arr); echo $arr[0]."<br>"; print $arr[2]."<br>"; ?>
Method 2: Output multiple array key values
First use the foreach statement to loop through the array
Use echo or print statements to output multiple array key values
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(11,0,12,2,3,4,5); var_dump($arr); foreach($arr as $v){ echo $v."<br>"; //print $v."<br>"; } ?>
Recommended study: "PHP Video Tutorial》
The above is the detailed content of How to output only array key values in php. For more information, please follow other related articles on the PHP Chinese website!