Number of elements in JSON

3

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

See my example 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, already tried to perform a json_decode before to try to manipulate json, but it always returns me a NULL. And with sizeof I just return the value 1.

    
asked by anonymous 08.06.2016 / 19:26

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
    
08.06.2016 / 19:33