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!

求和方法:1、用array_column()获取多维数组中指定一列的全部元素,语法“rray_column(数组, '指定列名')”,会返回一个包含全部元素的结果数组;2、用“array_sum(结果数组)”计算结果数组中所有元素的和即可。

增加元素的方法:1、使用array_push()函数,语法“array_push(二维数组,值1,值2...);”;2、使用array_splice()函数,语法“array_splice(二维数组,count(二维数组),0,元素值)”。

获取方法:1、用“array_values(数组)”将指定数组转为索引数组;2、用“array_search(数值,索引数组)”,在索引数组中搜索数值,返回对应的索引值(下标);3、用“索引值+1”语句获取元素在数组中的位置值。

php数组里面可以放数组。PHP数组可以存储所有类型的数据,当然也包括数组本身;如果一个数组中的元素是另一个数组,就构成了包含数组的数组,即多维度数组。数组的不同维度标志着需用几个下标(索引)来获取对应的数组元素,比如二维数组需用两个下标。

方法:1、循环遍历数组,语法“foreach($arr as $k=>$v){}”;2、循环体中,用“==”判断元素值是否为null,如果是则用unset()删除该元素,语法“if($v==null){unset($arr[$k]);}”。

php中只比较值的数组交集函数是“array_intersect()”;该函数用于比较两个(或更多个)数组的键值,语法“array_intersect(数组1,数组2...)”,会返回一个交集数组,所包含的值是从被比较的数组(数组1)中取。

去掉方法:1、用“sort($arr)”对数组进行升序排序,排序后该数组的第一个元素就是最小值,最后一个元素就是最大值;2、用“array_pop($arr)”删除最后一个元素,用“array_shift($arr)”删除第一个元素即可。

php中可用preg_match_all()配合正则表达式过滤字符串,只获取中文字符;语法“preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);”,会将匹配字符存入“$arr”数组中。


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools