Pick up json value

Asked

Viewed 1,262 times

0

1 answer

0


The problem lies in the fact that your json saved in the text document has an incorrect syntax or is not a json valid.

I do not know how you save in text but after each object and before closing them you put a comma, it is wrong so invalidates your document as json if I can correct this, a functional example for what you want would be something like this:

file.txt

 [ { "Nome": "Hemerson", "data": "05/11"

 }, { "Nome": "Hemerson", "data": "06/11"

 } ]

php test.

<?php
    // leia o arquivo
    $file = file_get_contents('file.txt');    

    // decodifique em array associativo para poder pegar a ultima posição
    $decode = json_decode($file, true);

    // pegue a ultima posição
    $ultimo = end($decode);

    // acesse o valor pela chave correspondente exemplo "data"
    echo $ultimo['data'];

    // isso ira imprimir: 06/11
  • Ola Lauro worked the decoding only one more doubt if I want to get the start instead of the last position as it would be put so it did not work $ultimo = start($Decode);

  • Hello friends solved instead of $last = end($Decode); that would be it : $last = reset ($Decode);

Browser other questions tagged

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