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
    • 563
    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

    $MyArray = array(
        'one' => firstelement,
        'two' => secondelement,
        'three' => lastelement, 
    );
    
    end($MyArray);        
    

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

     $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

    echo "$LastKey";
    

    Now this is much more easy syntex:-

     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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: