How can we get the last element of an array in php?

To get the last element of an array in php, the easiest way is with the end() function.

$array = array(1, 2, 3, 4, 5);

echo end($array);

// Output:
5

Another way to get the last element of an array is with the array_slice() function.

$array = array(1, 2, 3, 4, 5);

echo array_slice($array, -1)[0];

// Output:
5

A third way to get the last element of an array is with the array_pop() function.

$array = array(1, 2, 3, 4, 5);

echo array_pop($array);

// Output:
5

When working with arrays in php, it can be valuable to be able to easily filter and get only specific values from your array.

One such situation is when you want to get the last element from a php array.

There are a number of ways to get the last element from a php array, and the easiest way is with the end() function.

Below is an example of how to print the last element of a php array with end().

$array = array(1, 2, 3, 4, 5);

echo end($array);

// Output:
5

If you are looking to get the first element of a php array, the easiest way is by accessing the first element directly.

$array = array(1, 2, 3, 4, 5);

echo $array[0];

// Output:
1

Getting the Last Element of an Array in php with array_slice()

Another way we get the last element of an array in php with the php array_slice() function.

You can pass php array_slice() function a starting position and ending position, and get the elements in the array between those two positions.

To get the last element of an array in php, pass ‘-1’ to array_slice(). You don’t need to pass an ending position since by default the ending position is the end of the array.

Below is a simple example of how to get the last element of an array with array_slice() using php.

$array = array(1, 2, 3, 4, 5);

echo array_slice($array, -1)[0];

// Output:
5

Getting the Last Element of an Array in php with array_pop()

Another way to get the last element of an array is with the php array_pop() function.

The array_pop() function ‘pops’ and removes the last element from an array.

We can use array_pop() to return the last element of a php array.

Below is how to get the last element of a php array with array_pop().

$array = array(1, 2, 3, 4, 5);

echo array_pop($array);

// Output:
5

Getting the Last N Elements of an Array in php with array_slice()

If you are looking to get more than just the last element, we can get the last n elements of a php array with the array_slice() function.

You can pass php array_slice() function a starting position and ending position, and get the elements in the array between those two positions.

To get the last 3 elements of an array in php, pass ‘-3’ to array_slice(). We don’t need to pass an ending position since we will be going to the end of the array.

Below is a simple example of how to get the last three elements of an array using php.

$array = array(1, 2, 3, 4, 5);

echo var_dump(array_slice($array,-3));

// Output:
array(3) {
  [0]=>
  int(3)
  [1]=>
  int(4)
  [2]=>
  int(5)
}

Hopefully this article has been useful for you to learn how to get the last element of an array when using php.

In this post, we will see how to remove the first element from an array and return its value in PHP.

1. Removing last element using array_pop() function

The standard solution to remove the last element from an array is using the array_pop() function which return the value of the removed element. The time complexity of array_pop() is constant.

The following code pops the element off the end of the array using the array_slice() function, shortening the array by one element.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php

    $arr=[1, 2,3,4,5];

    $last=array_pop($arr);

    print_r("The last item is ".$last."\n");

    print_r($arr);

    /* Output:

        The last item is 5

        Array

        (

            [0] => 1

            [1] => 2

            [2] => 3

            [3] => 4

        )

    */

?>

Download  Run Code

 
For associative arrays:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

    $arr=array("Nick"=>"20","John"=>"10","Paul"=>"18");

    $last= array_pop($arr);

    print_r("The value of the last item is " .$last."\n");

    print_r($arr);

    /* Output:

        The value of the last item is 18

        Array

        (

            [Nick] => 20

            [John] => 10

        )

    */

?>

Download  Run Code

2. Removing first element using array_shift() function

If you need to remove the first element from an array, you can use the array_shift() function which deletes and return the first element in the array. The time complexity of array_shift() is linear since it has to run over all the elements in order to re-index them.

The following code shifts an element off the beginning of the array using the array_slice() function, shortening the array by one element.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php

    $arr=[1, 2,3,4,5];

    $first=array_shift($arr);

    print_r("The first item is ".$first."\n");

    print_r($arr);

    /* Output:

        The first item is 1

        Array

        (

            [0] => 2

            [1] => 3

            [2] => 4

            [3] => 5

        )

    */

?>

Download  Run Code

 
For associative arrays:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

    $arr=array("Nick"=>"20","John"=>"10","Paul"=>"18");

    $first= array_shift($arr);

    print_r("The value of the first item is ".$first."\n");

    print_r($arr);

    /* Output:

        The value of the first item is 20

        Array

        (

            [John] => 10

            [Paul] => 18

        )

    */

?>

Download  Run Code

That’s all about removing the first and last element from an array in PHP.

How do you find the last element of an array?

1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.

How do you find the last and first element in an array?

To get the first and last elements of an array, access the array at index 0 and the last index. For example, arr[0] returns the first element, whereas arr[arr. length - 1] returns the last element of the array.

How do I find the last index?

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex .

How do you move an element to the end of an array in PHP?

We use end() to move the array pointer to the last element.