Home >Backend Development >PHP Problem >How to output only array key values ​​in php

How to output only array key values ​​in php

青灯夜游
青灯夜游Original
2022-06-29 19:50:371288browse

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

How to output only array key values ​​in php

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>";
?>

How to output only array key values ​​in php

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

How to output only array key values ​​in php

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!

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