JSON 请求中返回的项目数的 PHP 计数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5923149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A PHP count of the number of items returned in JSON request?
提问by Rowan
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.
我正在寻找一种方法来计算我在搜索数据库时得到的这些 JSON 字符串中返回的项目数(在 PHP 中)。
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?
有人告诉我,因为 JSON 版本中没有返回计数,就像这个数据库中的 XML 一样,我必须使用循环来计算结果的数量?
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
如果适用,这是我用来请求和解密它的 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>';
}
回答by James
This should do the trick.
这应该可以解决问题。
$iCount = count($persons)
When you call json_decode
you're getting a PHP variable which contains an array of items and values.
当您调用时,json_decode
您将获得一个 PHP 变量,其中包含一组项目和值。
Currently you're getting what's called a stdClass
but if you add true
parameter to your json_decode
function, you'll get a normal array. Although using the true
parameter or not, you can still call count
:)
目前你得到了所谓的 astdClass
但如果你true
向你的json_decode
函数添加参数,你会得到一个普通的数组。不管是否使用true
参数,你仍然可以调用count
:)
$name = $_GET['search'];
$persons_result = $tmdb->searchPerson($name);
$persons = json_decode($persons_result, true);
print_r($persons);
There you go :)
你去吧:)
回答by Levi
If you run a query identical to below it will return the Item, and count unique items
如果您运行与下面相同的查询,它将返回项目,并计算唯一项目
$query = " SELECT table_column_here ,COUNT(*) FROM table_name GROUP BY table_column_here; ";
Which will return [{"column_name":"column_data_result","count":"1"}] via json.
这将通过 json 返回 [{"column_name":"column_data_result","count":"1"}]。
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.
您可以使用下面的代码,它会显示 json 结果。当然,您需要连接到 SQL 才能在下面使用。
$query = " SELECT table_column_here ,COUNT(*) FROM table_name GROUP BY table_column_here; ";
$result = mysql_query( $query );
if ( !$result ) {
$ErrorMessage = 'Invalid query: ' . mysql_error() . "\n";
$ErrorMessage .= 'Whole query: ' . $query;
die( $ErrorMessage );
}
$JSON_output = array();
while ( $row = mysql_fetch_assoc( $result ) )
{
$JSON_output[] = array('column_name' => $row['column_name'],
'count' => $row['COUNT(*)'],
);
}
I would assume that it would be easier this route, but could be wrong :) Hope it helps you or someone else out :)
我认为这条路线会更容易,但可能是错误的:) 希望它可以帮助您或其他人:)