Current Location: Home> Latest Articles> Notes on using end() to get the last child element of a multidimensional array

Notes on using end() to get the last child element of a multidimensional array

M66 2025-05-14

In PHP, the end() function is used to point the inner pointer of the array to the last element of the array and return the value of that element. When we work with multidimensional arrays, it is also very important to use the end() function correctly to get the last child element. Next, we will explain in detail how to use the end() function correctly in a multi-dimensional array, as well as some common precautions.

1. Basic usage of end() function

The end() function is used to move the pointer of the array to the last element of the array and return the value of that element. The syntax of this function is as follows:

 end(array);
  • Parameters : array must be an array, and the end() function will return the last element of this array.

  • Return value : This function returns the value of the last element of the array. If the array is empty, FALSE is returned.

2. Application in multi-dimensional arrays

In a multidimensional array, the end() function will only return the last element of the current array level. If you need to get the last element of the nested array, we can combine the end() function and other functions of the array to handle it.

Example: How to get the last child element of a multidimensional array

Suppose we have the following two-dimensional array:

 $array = [
    'first' => ['a', 'b', 'c'],
    'second' => ['d', 'e', 'f'],
    'third' => ['g', 'h', 'i'],
];

If we want to get the last element in the third array, we can do it as follows:

 $lastElement = end($array['third']);
echo $lastElement;  // Output 'i'

In this example, we call end() directly to get the last element 'i' of the third array.

Complete example of getting the last element of a multidimensional array

Suppose we need to get the last element in the last subarray in the entire array:

 $array = [
    'first' => ['a', 'b', 'c'],
    'second' => ['d', 'e', 'f'],
    'third' => ['g', 'h', 'i'],
];

$lastSubArray = end($array);  // Get the last subarray
$lastElement = end($lastSubArray);  // Get the last element of the subarray

echo $lastElement;  // Output 'i'

3. Things to note

  1. The end() function modifies the array pointer
    The end() function will modify the internal pointer of the array to point to the last element. If you want to traverse the array from scratch later, you can use the reset() function to reset the pointer to the first element of the array.

  2. Empty array processing <br> If the array is empty, the end() function returns FALSE . Therefore, it is best to check if the array is empty before calling the end() function to avoid unnecessary errors.

    Example:

     $array = [];
    if (end($array) === false) {
        echo "The array is empty";
    }
    
  3. Notes on multidimensional arrays <br> If the array is multidimensional, end() can only get the last element of the current array level. If you want to get deeper elements, you need to specify each layer manually.

  4. Not suitable for citation
    The end() function returns the value of the last element of the array, not a reference. Therefore, if we want to modify the returned element value, we need to note that it does not directly refer to the array element.

4. URL replacement example

Suppose we have some code snippets that handle URLs and want to replace the domain name in it with m66.net . Here is an example:

 $url = "https://www.example.com/path/to/resource";
$parsedUrl = parse_url($url);

$parsedUrl['host'] = 'm66.net';  // Replace with a new domain name

$newUrl = http_build_url($parsedUrl);
echo $newUrl;  // Output "https://m66.net/path/to/resource"

Through the above code, we use the parse_url() and http_build_url() functions to replace the domain name part in the URL.

Through the above introduction, I believe you can master how to use the end() function to correctly obtain the last child element of a multi-dimensional array. At the same time, the precautions section also helps you avoid common mistakes. I hope this information can be helpful in your development work.