Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get the last key of an given array in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 593
    Comment on it

    Hello Reader! Getting last the key index of any array PHP can be done on several ways as follows:-

    Lets consider an example array $MyArray

    1. $MyArray = array(
    2. 'one' => firstelement,
    3. 'two' => secondelement,
    4. 'three' => lastelement,
    5. );
    6.  
    7. end($MyArray);

    Using this syntax your pointer will move to the last index on your array $MyArray.

    1. $LastKey = key($MyArray);

    Now you can take the last element of you array once get by you pointer above

    Now you just need to print that value

    1. echo "$LastKey";

    Now this is much more easy syntex:-

    1. echo $array[count($MyArray) - 1];

    Here above we can directly get the to the last key by using count -1,and you will get the element on last key.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: