PHP 计算 JSON 请求中返回的项目数?
我正在寻找一种方法来计算在搜索数据库时得到的这些 JSON 字符串中返回的项目数(以 PHP 表示)。
请原谅我,因为我对这一切一无所知。
有人告诉我,因为 JSON 版本中没有返回计数,就像此数据库中的 XML 版本一样,我必须使用循环来计算结果数?
我环顾四周,但似乎没有什么适合我想做的...
以下是我得到的字符串的示例:
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
)
)
如果适用,这是我用来请求 & 的 php破译它
$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>';
}
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>';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
上使用
获取项目数量。count
函数$personsUse the
count
function on$persons
to get the number of items.这应该可以解决问题。
当您调用 json_decode 时,您将获得一个包含项目和值数组的 PHP 变量。
目前,您获得的是所谓的 stdClass,但如果您将 true 参数添加到 json_decode 函数,您将获得普通数组。尽管是否使用
true
参数,您仍然可以调用count
:)就这样 :)
This should do the trick.
When you call
json_decode
you're getting a PHP variable which contains an array of items and values.Currently you're getting what's called a
stdClass
but if you addtrue
parameter to yourjson_decode
function, you'll get a normal array. Although using thetrue
parameter or not, you can still callcount
:)There you go :)
如果您运行与下面相同的查询,它将返回项目,并计算唯一项目
,这将通过 json 返回 [{"column_name":"column_data_result","count":"1"}]。
您可以使用下面的代码,它会显示 json 结果。当然,您需要连接到 SQL 才能在下面使用。
我认为这条路线会更容易,但可能是错误的:)希望它可以帮助你或其他人:)
If you run a query identical to below it will return the Item, and count unique items
Which will return [{"column_name":"column_data_result","count":"1"}] via json.
You can use the code below, and it will display the json result back. Of course you will need to Connect to SQL to use below.
I would assume that it would be easier this route, but could be wrong :) Hope it helps you or someone else out :)