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.
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:
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:
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).'<br>'; echo pos($arr).'<br>'; next($arr); echo current($arr).'<br>'; next($arr); next($arr); echo current($arr); ?>
Output result:
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:
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:
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!