5 methods: 1. Use the "array_pop($arr)" statement to delete the last element of the array and return the value; 2. Use the "end($arr)" statement to return the value of the last element; 3. , Use the "array_slice($arr,-1)" statement to intercept the last 1 element of the array; 4. Use the "array_splice($arr,-1)" statement to delete the last element and return the element value; 5. Use "$arr[ count($arr)-1]" statement returns the last element.

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
php gets the final array One-digit element value method
1. Use array_pop() function
array_pop() function will delete the last element in the array. and returns the deleted element.
<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
var_dump($arr);
//获取数组中的第一个元素
$first = array_pop($arr);
echo "数组的最后一个元素为:".$first;
?>
2. Use the end() function
end() function to point the pointer inside the array to the last one of the array element and returns the value of the last element, or FALSE if the array is empty.
<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉"=>"3元","苹果"=>"5元","梨子"=>"6元","橙子"=>"4元","橘子"=>"3元","榴莲"=>"23元");
var_dump($arr);
//获取数组中的第一个元素
$last = end($arr);
echo "数组最后一个元素的键名和键值:".key($arr)." ".$last;
?>
3. Use the array_slice() function
If you want to intercept the last few elements of the array, just set the parameter start For -1
<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);
echo "截取最后一位元素:";
$result = array_slice($arr,-1); //截取数组后2位的元素
var_dump($result);
?>
4, use the array_splice() function
array_splice() function to delete When a part of the array is removed, the deleted elements will be formed into a new array, and then the new array will be returned; therefore, the array_splice() function can be used to intercept array fragments.
Same as the array_slice() function, just set the second parameter start of the function to -1 to intercept the last element.
<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);
echo "截取最后一位元素:";
$result = array_splice($arr,-1);
var_dump($result);
?>
5. Use array name[subscript] to access the last array element
subscript The value needs to be set to array length-1, that is, count($arr)-1
<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,12,20,25,24);
var_dump($arr);
echo "最后一个元素:".$arr[count($arr)-1];
?>
Note: This method Can only be used in indexed arrays.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to find the value of the last element of an array in php. For more information, please follow other related articles on the PHP Chinese website!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand
PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.
PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment






