Doubt PHP with JSON: I already know how to loop in a JSON file and show all records and also show only the last record using the END. How can I show the last 5, 10 record, for example?
Doubt PHP with JSON: I already know how to loop in a JSON file and show all records and also show only the last record using the END. How can I show the last 5, 10 record, for example?
To use a portion of an array, just use array_splice
.
$input = array( "vermelho" , "verde" , "azul" , "amarelo" , "preto" , "cinza" );
array_splice( $input , 0 , -5 );
print_r( $input );
output
Array
(
[0] => verde
[1] => azul
[2] => amarelo
[3] => preto
[4] => cinza
)
That's not what I need. I have a JSON file and I already know how to loop and return all records. If I only need the last record I use the END like this:
$pesquisa = json_decode(file_get_contents("nomedoarquivo"),true);
$ultimoRegistroPesq = end($pesquisa["registros"]);
$strCampo = $ultimoRegistroPesq['campo'];
I want to know if you can limit your search to the last 5 records, for example. If I were searching for a database I would use LIMIT 5 in the query. But with JSON I do not know.