Suraj Roy Suraj Roy - 1 year ago 112
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download