[英]A PHP count of the number of items returned in JSON request?
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>';
}
使用$persons
上的count
函数来获取项目数。
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. 目前你正在获得所谓的
stdClass
但是如果你在你的json_decode
函数中添加true
参数,你将获得一个正常的数组。 Although using the true
parameter or not, you can still call count
:) 虽然使用
true
参数与否,你仍然可以调用count
:)
$name = $_GET['search'];
$persons_result = $tmdb->searchPerson($name);
$persons = json_decode($persons_result, true);
print_r($persons);
There you go :) 你去:)
If you run a query identical to below it will return the Item, and count unique items 如果您运行与下面相同的查询,它将返回Item,并计算唯一项
$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. 您可以使用下面的代码,它将显示json结果。 Of course you will need to Connect to SQL to use below.
当然,您需要连接到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 :) 我会认为这条路线会更容易,但可能是错误的:)希望它可以帮助你或其他人:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.