Get the first & last elements of an associative array

Get the first & last elements of an associative array

This is a small tutorial on how to get the first elements of an associative array in PHP. As you probably already know, associative arrays are extremely popular in PHP, simply because they allow you to set keys/indexes that are human-friendly! Unfortunately, one of the drawbacks to using an associative array is that it can be difficult to retrieve elements based on their position!

Note that we will do this without having to loop through the entire array (a wasteful process).

For this tutorial, We are using the following associative array as an example:

$array = array( 
    'key_1' => 'Value #1', 
    'key_2' => 'Value #2', 
    'key_3' => 'Value #3' 
);

Getting the first element of an associative array

Getting the first element is pretty easy if you understand the reset function. This function allows you to “reset” the internal pointer of an array back to its first element:

//Example associative array.
$array = array(
    'key_1' => 'Value #1',
    'key_2' => 'Value #2',
    'key_3' => 'Value #3'
);
 
//Use the reset function to "set the internal pointer of an array to its first element".
$firstElement = reset( $array );
 
//var_dump the result for illustrative purposes.
echo $firstElement;

If you run the code snippet above, you will see that the reset function returns the first element of an array, regardless of whether it is an associative array or not.

Getting the first key of an associative array

This is another common problem that developers face when working with associative arrays. When working with numbered indexes, it is pretty safe to assume that the first key will be “0”. However, with an associative array, it isn’t that easy.

//The same example array.
$array = array(
    'key_1' => 'Value #1',
    'key_2' => 'Value #2',
    'key_3' => 'Value #3'
);
 
//Reset the array back to the start.
reset( $array );
 
//Fetch the key from the current element.
$key = key( $array );

In the example above, we combined the reset function and the key function in order to get the first key from our array.

Getting the last element of an associative array

To get the last element, we are going to use the PHP function end, which changes the internal pointer of an array to its last element:

//Our example array.
$array = array( 
    'key_1' => 'Value #1', 
    'key_2' => 'Value #2', 
    'key_3' => 'Value #3' 
);
 
//Set the internal pointer to the end of $myArray.
$lastElement = end( $array );
 
//Print it out!
echo $lastElement;

Simple!

Getting the last key of an associative array

But what if we want to get the key/index of the last element? Well, it’s kind of similar:

//Associative PHP array.
$array = array( 
    'key_1' => 'Value #1', 
    'key_2' => 'Value #2', 
    'key_3' => 'Value #3' 
);

//Set the internal pointer to the end.
end( $array );
 
//Retrieve the key of the current element.
$key = key( $array );
 
//Print it out!
echo $key;

As you can see, using the end function and the key function is a lot less wasteful than looping over the entire array!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.