How to take the penultimate and antipenultímo item from an array

Asked

Viewed 2,570 times

4

I have a array that comes from a return of a form and would like to know how to get the penultimate and antepenultimate position of the same in PHP.

4 answers

7


Basically the count() to get the number of items it has in the array, although there are other options.

Being for example:

$array = ['um', 'dois', 'tres', 'quatro', 'cinco', 'seis'];

Can get the last using:

$array[ count($array) - 1  ];

Therefore, to get the penultimate and the penultimate is only mathematical, instead of -1 utilize - 2 and - 3, for example:

$penultimo = $array[ count($array) - 2  ];
$antepenultimo = $array[ count($array) - 3  ];

Test it out here.


Another option is to use the end() and then the prev(), in one instance:

$array = ['um', 'dois', 'tres', 'quatro', 'cinco', 'seis'];    

$ultimo = end($array);
$penultimo = prev($array);
$antepenultimo = prev($array);

reset($array);

Test it out here.

The end will move to the last item, then the prev() will get the previous.


Another way to get it is by using the array_slice, for example:

$array = ['um', 'dois', 'tres', 'quatro', 'cinco', 'seis'];  

list($antePenultimo, $penultimo, $ultimo) = array_slice($array, -3);

Test it out here.

In this case it is a little different, the array_slice will create a new array, but it will contain only the last three values due to the -3.

If you only want the antepenultimate and penultimate you can set a length, which is the third parameter, thus defining 2 will get antepenultimate, for example:

list($antePenultimo, $penultimo) = array_slice($array, -3, 2);

If you want to reverse the order use the array_reverse() and if you want to preserve the keys add the true in the fourth parameter, for example:

foreach(array_reverse(array_slice($array, -3, 2)) as $texto){
    echo $texto;
    echo '<br>';
}

Test it out here.

  • Good, very thorough answer, thanks

4

Some more alternatives:

$array = ['um', 'dois', 'tres', 'quatro', 'cinco', 'seis'];

$ultimoAntepenultimo = array_splice( $array, -3, 2 );

(use the slice in place of splice if you don’t want to remove from the original)

See working on IDEONE.

If you want to separate, and have no problem removing from the original, you can use this:

$array = ['um', 'dois', 'tres', 'quatro', 'cinco', 'seis'];

$ultimo = array_pop( $array );
$penultimo = array_pop( $array );
$antePenultimo = array_pop( $array );

See working on IDEONE.

The advantage in this case is not to create new array.

4

Another way to do this is to combine the functions end() which puts the array pointer in the last position and calls prev() which puts it in the previous position so this gives the penultimate position. I used reset() in the example to put the array pointer at the beginning.

$arr = array(1, 2, 3, 4, 5, 6, 8, 9);
end($arr);
$penultimo = prev($arr);
$antepenultimo = prev($arr);

echo 'Penultimo: '. $penultimo .' | Antepenultimo: '.$antepenultimo;
  • Very good alternative.

2

Array[0] // primeiro item
Array[Array.length - 1] // Ultimo item

//No caso do PHP acho que é mais ou menos isso
$Array[0]
$Array[count($Array) - 1]

I use it that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.