Home  >  Article  >  Backend Development  >  Why does php array have no key value?

Why does php array have no key value?

青灯夜游
青灯夜游Original
2021-09-09 19:13:231736browse

Value method: 1. Use the "array_shift($arr)" statement to obtain the value; 2. Use the "array_pop($arr)" statement; 3. Use the "current($arr)" statement; 4. Use the "reset($arr)" statement; 5. Use the "end($arr)" statement.

Why does php array have no key value?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php array has no key The value

Method 1: Use array_shift() to get the value of the first element in the array

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");

//获取数组中的第一个元素
$first = array_shift($arr);
echo "数组的第一个元素的值为:".$first;
?>

Output result:

Why does php array have no key value?

Method 2: Use array_pop() to get the value of the last element in the array

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");

//获取数组中的最后一个元素
$last = array_pop($arr);
echo "数组的最后一个元素的值为:".$last;
?>

Output result:

Why does php array have no key value?

Method 3: Use current() to return the value of the current element of the array

In PHP, each array has an internal pointer pointing to it Its "current" element (element), this pointer initially points to the first element in the current array.

Through the current() function, you can get the value of the element pointed to by the internal pointer.

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");

echo current($arr).&#39;<br>&#39;;
echo pos($arr).&#39;<br>&#39;;
next($arr);
echo current($arr).&#39;<br>&#39;;
next($arr);
next($arr);
echo current($arr);
?>

Output result:

Why does php array have no key value?

Method 4: reset() function gets the value of the first element of the array

reset() function: You can point the internal pointer in the array to the first element and return the value of the element.

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");

$first = reset($arr);
echo "数组第一个元素的值:" .$first;
?>

Output result:

Why does php array have no key value?

Method 5: end() function gets the value of the last element of the array

end() function: You can point the internal pointer in the array to the last element and return the value of that element.

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");

$last = end($arr);
echo "数组最后一个元素的值:".$last;
?>

Output results:

Why does php array have no key value?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Why does php array have no key value?. 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