php - How to properly count json contents? -


this json

[     {"id":"1736375","first_name":"fname1","force_first_name":"ffname1","last_name":"lname1","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},     {"id":"1607011","first_name":"fname2","force_first_name":"ffname2","last_name":"lname2","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},     {"id":"1607012","first_name":"fname3","force_first_name":"ffname3","last_name":"lname3","thumb_path":"","path":"img\/profiles\/generic\/gray.png"} ] 

i trying count number of sets inside [ ]

i tried using this

echo count(json_decode($people, true)); 

i 0 (0) result.

how can count it.

thanks

=== edit sake of future viewer ===

it json malformed stated several comments, code wrote above how see real content of json this

string(3)" [     {"id":"1736375","first_name":"fname1","force_first_name":"ffname1","last_name":"lname1","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},     {"id":"1607011","first_name":"fname2","force_first_name":"ffname2","last_name":"lname2","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},     {"id":"1607012","first_name":"fname3","force_first_name":"ffname3","last_name":"lname3","thumb_path":"","path":"img\/profiles\/generic\/gray.png"} ]" 

as pointed out @dontpanic, string return 1 get. reported problem developer , luckily corrected json response , working ok.

thanks made effort comment leading discovery of problem.

using exact code desired output of 3, other comments above. i'd recommend debugging $people variable ensure remaining json object way through until echo statement, quite possible either being malformed or changed together, therefore giving unexpected results.

<?php $people = <<<eod [     {"id":"1736375","first_name":"fname1","force_first_name":"ffname1","last_name":"lname1","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},     {"id":"1607011","first_name":"fname2","force_first_name":"ffname2","last_name":"lname2","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},     {"id":"1607012","first_name":"fname3","force_first_name":"ffname3","last_name":"lname3","thumb_path":"","path":"img\/profiles\/generic\/gray.png"} ] eod;  echo count(json_decode($people, true)); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -