halaxa / json-machine

Efficient, easy-to-use, and fast PHP JSON stream parser
Apache License 2.0
1.08k stars 65 forks source link

total items #69

Closed AlessandroCuba closed 2 years ago

AlessandroCuba commented 2 years ago

Hi,

How can I know, how many Items has the JSON?

halaxa commented 2 years ago

Hi, as said in the TL;DR section, you have to iterate over the whole thing and count the items yourself. It is not possible to know the count beforehand as this is a lazy parser.

$count = 0;
foreach (Items::fromFile('file.json') as $item) {
    $count++
}
echo $count;
halaxa commented 2 years ago

Also, you can use iterator_count() which will do that for you. Just use iterator_count($items). Remember it will still have to iterate the whole thing to get the count and thus will take about the same time.