Return n last record of a JSON file in php

Asked

Viewed 98 times

0

PHP doubt with JSON: I already know how to loop a JSON file and show all records and also show only the last record using END. Can I show the last 5, 10 record, for example?

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
)
  • It is not to return part of an array. This I know. It is to return an N amount of record of a JSON file, not all records. Got it?

  • That. In the example I returned only the 5 latter.

0

This is not what I need. I have a JSON file and already know how to loop and return all records. If I just need to last record use the END so:

    $pesquisa = json_decode(file_get_contents("nomedoarquivo"),true);
    $ultimoRegistroPesq = end($pesquisa["registros"]);
    $strCampo = $ultimoRegistroPesq['campo'];

I want to know if you can limit the search to the last 5 records, for example. If you were searching a database I would use LIMIT 5 in the query. But with JSON I don’t know.

  • You don’t need to create an answer, edit your question if you need to add any information about the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.