Return last n record of a JSON file in php

0

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?

    
asked by anonymous 10.07.2018 / 04:07

2 answers

0

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
)
    
10.07.2018 / 04:32
0

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.

    
10.07.2018 / 04:40