What if I told you that you can filter this matrix, without injecting it, at least not in the conventional way?
Nothing against the solution presented but should be avoided at all costs nested loops. This is extremely damaging to the performance of the Application.
Translating your problem into smaller parts, you want to locate a particular index and return everything after it.
By doing this, you are removing some items from the original matrix, so you are filtering it. What if you are filtering an array, array_filter() is the solution.
You’re gonna need one callback customized since there is no native function to do what you need. The... "essence" of this callback will be based on function key():
$filtered = array_filter(
$array,
function( $current ) {
return ( key( $current ) > 2 );
}
);
This produces:
Array
(
[2] => Array
(
[3] => Array
(
[id] => 1264
[nome] => Fulano 1
[sobrenome] => de Tal1
)
)
[3] => Array
(
[4] => Array
(
[id] => 1267
[nome] => Fulano 2
[sobrenome] => de Tal2
)
)
)
Note: The focus of the answer is filter the matrix and not unidimensionalize it, secondary and subsequent task of extreme importance to keep the routine away from nested loops.
When you say after the second key you mean after the second position?
– Silvio Andorinha
That’s right, after the second position!
– Diego Henrique
Friend just to confirm, you want in js or php?
– Silvio Andorinha
Considering the @Silvioandorinha question, so no matter what the key is in the second position, you want to generate another array with the contents from the third. That’s right?
– bfavaretto
@Silvioandorinha It is in php, sorry for the lack of information!
– Diego Henrique
@bfavaretto I don’t want to do another array, I want to print the values from the third position.
– Diego Henrique