You can use the functions reset()
and end()
, this way a new matrix will be created with the first and last element of the main matrix. Using the foreach()
you will read all the contents of the new matrix created and use the same functions to capture the first and last element of each array of the matrix.
<?php
$matriz = [
[1,3,5],//1
[7,9,11],//0,2
[13,15,17]//1
];
$matrizPrimeiroUltimo[] = reset($matriz);
$matrizPrimeiroUltimo[] = end($matriz);
foreach ($matrizPrimeiroUltimo as $valor) {
$primeiroUltimo[] = reset($valor);
$primeiroUltimo[] = end($valor);
}
var_dump($primeiroUltimo);
PHP documentation reset()
reset() returns the internal array pointer to the first element and returns the value of the first element of the array, or FALSE if the array is empty.
PHP end documentation()
end() advances the array internal pointer to its last element, and returns it.
PHP foreach documentation()