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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 718
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss how to get last key in an array in PHP ?

    we need in some cases the last key or last element of an array in PHP.
    So We are using array_slice() function for getting only the last element of the array.
    It is very easy and fast function for getting last element of the array

    you can see bellow example:

    <?php
    //here define a variable of array
       $data = array(
        'Emp_name' => 'mac',
        'Emp_age' => 35,
        'Emp_dep' => 'account', 
    );
     //here define a variable and call array_slice() to get last element of the array
    $last_key = key( array_slice( $data, -1, 1, TRUE ) );
    //here print $last_key variable
    echo $last_key;
    
    ?>
    

    output will come following of above example

    Emp_dep  
    

 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: