Home >Backend Development >PHP Problem >How to find the value of the last element of an array in php

How to find the value of the last element of an array in php

青灯夜游
青灯夜游Original
2022-05-10 18:48:086893browse

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.

How to find the value of the last element of an array in php

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

How to find the value of the last element of an array in php

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

How to find the value of the last element of an array in php

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

How to find the value of the last element of an array in php

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

How to find the value of the last element of an array in php

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

How to find the value of the last element of an array in php

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!

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