php - Trying to count the length of an array from JSON list -


i trying count length of array converted json using json_decode in php not working reason. current code. json list contains array has 10,000 items. pretty sure missing something. appreciated.

php

<?php $fl = file_get_contents($somepath); $text = json_decode($fl, true);  $len = count($text["alphalist"]);  echo $len; ?> 

json

{ "alphalist": [{         "a": "alphabet1."     }, {         "b": "alphabet2."     }, {         "c": "alphabet3."     }, {         "d": "alphabet4."     }, {         "e": "alphabet5."     }     ....     {         "zzzzz": "alphabet10000."     } ] } 

the answer here in response similar question.

php not converting json using 'json_decode()'

so after testing user d4v1d, turns out problem due php function json_decode not working because wasn't in utf-8 format. workaround is

$fl = file_get_contents($somepath); $text = json_decode(utf8_encode($fl), true); $len = count($text["alphalist"]); 

now $len give correct array length. if there error or improvement made in explanation, place in comments , correct accordingly.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -