PHP 计算 JSON 请求中返回的项目数?

发布于 2024-11-05 12:27:07 字数 1176 浏览 0 评论 0原文

我正在寻找一种方法来计算在搜索数据库时得到的这些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思碎 2024-11-12 12:27:07

上使用 count 函数$persons 获取项目数量。

Use the count function on $persons to get the number of items.

葬﹪忆之殇 2024-11-12 12:27:07

这应该可以解决问题。

$iCount = count($persons)

当您调用 json_decode 时,您将获得一个包含项目和值数组的 PHP 变量。

目前,您获得的是所谓的 stdClass,但如果您将 true 参数添加到 json_decode 函数,您将获得普通数组。尽管是否使用 true 参数,您仍然可以调用 count :)

$name = $_GET['search'];
$persons_result = $tmdb->searchPerson($name);
$persons = json_decode($persons_result, true);
print_r($persons);

就这样 :)

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.

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 :)

$name = $_GET['search'];
$persons_result = $tmdb->searchPerson($name);
$persons = json_decode($persons_result, true);
print_r($persons);

There you go :)

荒路情人 2024-11-12 12:27:07

如果您运行与下面相同的查询,它将返回项目,并计算唯一项目

$query = " SELECT table_column_here ,COUNT(*) FROM table_name GROUP BY table_column_here;  ";

,这将通过 json 返回 [{"column_name":"column_data_result","count":"1"}]。

您可以使用下面的代码,它会显示 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(*)'],
                        );
    }

我认为这条路线会更容易,但可能是错误的:)希望它可以帮助你或其他人:)

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.

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.

$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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文