首页  >  文章  >  后端开发  >  PHP 获取数组的最后一个元素

PHP 获取数组的最后一个元素

WBOY
WBOY原创
2024-08-29 12:43:49386浏览

PHP 获取数组的最后一个元素是指使用 end() 函数获取关联数组中的最后一个元素。 PHP 中的 end() 函数是一个内置函数,具有独特的功能,可以操作和使用与数组关联的内部指针来指向数组的最后一个元素,然后从数组中返回该最后一个元素。这个用于获取最后一个元素的内置函数与一些特定的 PHP 版本兼容,例如 PHP 版本 4、PHP 版本 5 和 PHP 7。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

获取数组最后一个元素的语法流程如下:

end($src_array)
  • end (): 这是一个允许从数组(即 src_array)传递参数的函数。
  • src_array: 这是从 end() 函数传递的参数,用于获取所需的最后一个元素。
  • 返回类型:上述函数的返回类型为 true 或 false,这表示如果值为 true,则返回数组中的最后一个元素,如果值为 false,那么返回的值将是一个空数组,没有任何返回类型。

如何在 PHP 中获取数组的最后一个元素?

在 PHP 中获取数组最后一个元素的方法有多种,但出于需求,还是推荐一些方法。 PHP 中元素和数组索引的获取和操作都是通过遍历来完成的。因此,这取决于需要如何操作数据。

PHP中获取数组最后一个元素的方法如下:

  • 最初,应该正确遍历定义的数组,以使用 end() 函数获取最后一个元素。
  • End() 函数是 PHP 中的内置函数,广泛用于通过指向数组最后一个元素的内部指针变化来获取最后一个元素,这意味着它完全取决于返回类型,这有其自身的意义.
  • 如果 end 函数给出的值为 true,则可以说最后一个元素已正确获取。但如果 end 函数给出的值为 false,则表示返回的值为空,而没有获取最后一个元素。
  • 在 PHP 中获取数组最后一个元素的另一种方法是使用 pop() 函数,这意味着数组的最后一个元素很容易被删除,但存在一些特殊情况,例如数组为空且不是数组时数组,那么返回的值将为 NULL。否则,它将返回该值。
  • 此外,数组的最后一个元素可以从多维数组中获取,这意味着如果 PHP 中数组的最后一个元素被识别,那么数组最后一列中存在的值可以是使用索引键和列键拉取和操作以获取列的最后一个元素。
  • 为了将列与数组列表中的键元素映射,需要主键。
  • 识别多维数组的正确方法是将其与关联数组及其功能进行比较。

PHP 获取数组最后一个元素的示例

下面提到了不同的示例:

示例#1

该程序用于演示通过使用 end() 函数打印数组的最后一个元素来检索数组的最后一个元素,如图所示。源数组的输入是水果列表,要获取的最后一个元素是 Banana,如输出所示。

代码:

<?php
$arr = array('Apple', 'Guava', 'Banana');
echo end($arr);
?>

输出:

PHP 获取数组的最后一个元素

示例#2

该程序演示了 PHP 中的数组集,用于从数据库中获取与列相关的数据,然后在索引列中提供相关元素,以从该列中检索最后命名的值,如下所示在输出中。

代码:

<?php
$rcrds = array(
array(
'id' => 1124,
'1st_nm' => 'Ani',
'lst_nm' => 'Jo',
),
array(
'id' => 2356,
'1st_nm' => 'Samm',
'lst_nm' => 'Brack',
),
array(
'id' => 7890,
'1st_nm' => 'Brown',
'lst_nm' => 'Dan_l',
),
array(
'id' => 5623,
'1st_nm' => 'Ptr',
'lst_nm' => 'Katty',
)
);
$last_nm = array_column($rcrds, 'lst_nm');
print_r($last_nm);
?>

输出:

PHP 获取数组的最后一个元素

示例#3

该程序演示了 PHP 中的 pop() 函数,该函数专门演示了如何使用操作从数组中获取最后一个元素,首先获取列表,然后返回列表中的实际值作为最后一个元素,如下所示输出。 end() 函数和 pop() 函数的使用如输出所示。

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$arr_lst=array("magenta","green","red","blue","pink");
array_pop($arr_lst);
print_r($arr_lst);
?>
</body>
</html>

Output:

PHP 获取数组的最后一个元素

Example #4

This program demonstrates the difference between the last key and end function simultaneously to fetch the last value from an array by consuming a set of arrays as input and giving a string of length 3 as output as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$ar_lst = array(
'1st' => 151,
'2nd' => 234,
'lst' => 657,
);
end($ar_lst);
$key_set = key($ar_lst);
var_dump($key_set);
?>
</body>
</html>

Output:

PHP 获取数组的最后一个元素

Note: A mere recommendation for the above program to get the last element of an array is the combination of both the end() function and key() function, where the end() function will be used to return the last element by advancing the array’s internal pointer followed by returning the value. Similarly, the key() function will also return the index element of the current array.

Conclusion

PHP’s last element of an array is fetching the last element from an array using certain functions in PHP like the end() function, pop() function. Again, it all depends on the requirement of fetching and the manipulation needed to be performed on the array set. Also, an internal pointer within the array can be used as a pointer for performing the operation.

以上是PHP 获取数组的最后一个元素的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP in_array下一篇:PHP object to array