How to get the penultimate and anti-second item in an array

4

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

asked by anonymous 24.04.2017 / 16:39

4 answers

6

Basically you use count() to get the number of items you have in the array, although there are other options.

For example:

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

You can get the last using:

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

So, to get the penultimate and the penultimate is just mathematics, instead of -1 use - 2 and - 3 , for example:

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

Try this here.

Another option is to use end() and then prev() , in an example:

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

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

reset($array);

Try this here.

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

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

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

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

Try this here.

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

If you want only the penultimate and penultimate you can define a length , which is the third parameter, thus setting to 2 will get penultimate, for example:

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

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

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

Try this here.

    
24.04.2017 / 16:51
4

Here are some alternatives:

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

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

(use slice instead of splice if you do not want to remove from the original)

See working at 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 at IDEONE .

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

    
24.04.2017 / 17:08
4

Another way to do this is to combine the end() functions that puts the array pointer in the last position and call 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;
    
24.04.2017 / 17:21
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.

    
24.04.2017 / 16:43