I am looking for a way to count the number of items (in PHP) returned in these JSON strings I am getting when searching a database.
Forgive me because I’m utterly crap at all of this.
I was told because there is no count returned in the JSON version, like there is in the XML one from this database, I would have to use a loop to count the number of results?
I’ve had a look around but nothing seems to fit what I want to do…
The following is an example of the strings I get:
Array (
[0] => stdClass Object (
[score] => 12
[popularity] => 3
[name] => Samantha Dark
[id] => 151019
[biography] => [url] => http://www.themoviedb.org/person/151019
[profile] => Array ( )
[version] => 16
[last_modified_at] => 2011-04-11 17:17:33
)
[1] => stdClass Object (
[score] => 11
[popularity] => 3
[name] => Johnny Dark
[id] => 158737
[biography] => [url] => http://www.themoviedb.org/person/158737
[profile] => Array ( )
[version] => 14
[last_modified_at] => 2011-04-11 17:18:38
)
)
and if applicable, here’s the php I use to request & decipher it
$name = $_GET['search'];
$persons_result = $tmdb->searchPerson($name);
$persons = json_decode($persons_result);
foreach ($persons as $person) {
echo '<a href="tmdb_person.php?id='.$person->id.'">'.$person->name.'</a></br>';
}
Use the
countfunction on$personsto get the number of items.