Suraj Roy Suraj Roy - 3 months ago 19
PHP Question

Count number of objects in json php?

i have a json array and in that i have 3 json objects. i want to count number of objects that is 3. but it is giving me 1. if i do not add the key "like", then it works. but after adding it it is not working.

$JSON = '{"like":['
. '{"username":"suraj","password":"abc"},'
. '{"username":"don","password":"abc"},'
. '{"username":"rana","password":"abc"}'
. ']}';


$jsonInPHP = json_decode($JSON);
echo count($jsonInPHP);

Answer

your json represent a object not a array. In your object you have like property which is an array so you need to right like this

count($jsonInPHP->like);