Last Updated: February 25, 2016
·
8.594K
· webfacer

Get last elements key in PHP

When you want to check if you at the end of an array for whatever reason you can use this short phpline:

<?php
    $arr = array(1, 'test', 'i am the last element');
    $lastElementKey = array_pop(array_keys($arr));

it throws you the last key of the Array (in our case it will throw the key 2 out).

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside