Reputation: 345
I am trying to count the length of an array that I converted from JSON using json_decode
in php but it is not working for some reason. This is my current code. The JSON list contains an array that has 10,000 items. I am pretty sure that I am missing something. Any help will be greatly 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."
}
]
}
Upvotes: 1
Views: 244
Reputation: 345
The answer was actually here in response to another similar question.
PHP not converting JSON using 'json_decode()'
So after some testing with user D4V1D, it turns out that the problem was due to the PHP function json_decode
not working because it wasn't in the UTF-8 format. The workaround for this is
$fl = file_get_contents($somepath);
$text = json_decode(utf8_encode($fl), true);
$len = count($text["alphalist"]);
Now $len
will give the correct array length. If there is any error or improvement to be made in my explanation, just place it in the comments and I will correct it accordingly.
Upvotes: 1