Amount of elements in JSON

Asked

Viewed 753 times

3

How can I find out the amount of elements of a json file through PHP.

See the example of my file:

[{"descricao":"Fotografia","codigo":784},{"descricao":"Filmagem","codigo":789}]

I would like to check the amount of elements, which in this case should be 2, I tried to perform a json_decode before to try to manipulate the json, but always returns me a NULL. And with the sizeof returns me only the value 1.

1 answer

3


Do the json_decode in the json string and use Count in the result:

$parseJson = json_decode('[{"descricao":"Fotografia","codigo":784},{"descricao":"Filmagem","codigo":789}]');
echo count($parseJson); // 2
  • was what I had been trying, but it only works if I use a utf8_encode($str_json) before json_decode(). Vlw

  • Did you get @tkmattos? I just really tested with this json and it’s okay

  • If you’re having to use utf8_enconde(), then why not open the file with the notepad and check if it is saved as ANSI. If you are switching to UTF-8 and remove PHP functions.

  • It worked @Miguel, perfect

  • @Eduardoalmeida json comes from a webservice

  • You’re welcome @tkmattos

Show 1 more comment

Browser other questions tagged

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