php擷取數組的最後一個元素

來源:互聯網
上載者:User

php擷取數組的最後一個元素的方法。使用php內建函數end()。

end() 函數將數組內部指標指向最後一個元素,並返回該元素的值(如果成功)。例如:

$args=Array('www','phpernote','com');echo end($args);//com

相關的方法:
current() - 返回數組中的當前元素的值
next() - 將內部指標指向數組中的下一個元素,並輸出
prev() - 將內部指標指向數組中的上一個元素,並輸出
reset() - 將內部指標指向數組中的第一個元素,並輸出
each() - 返回當前元素的鍵名和索引值,並將內部指標向前移動

示範所有相關的方法:

<?php$people = array("Bill", "Steve", "Mark", "David");echo current($people) . "<br>"; // 當前元素是 Billecho next($people) . "<br>"; // Bill 的下一個元素是 Steveecho current($people) . "<br>"; // 現在當前元素是 Steveecho prev($people) . "<br>"; // Steve 的上一個元素是 Billecho end($people) . "<br>"; // 最後一個元素是 Davidecho prev($people) . "<br>"; // David 之前的元素是 Markecho current($people) . "<br>"; // 目前的當前元素是 Markecho reset($people) . "<br>"; // 把內部指標移動到數組的首個元素,即 Billecho next($people) . "<br>"; // Bill 的下一個元素是 Steveprint_r (each($people)); // 返回當前元素的鍵名和索引值(目前是 Steve),並向前移動內部指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.