dozpox8752 2015-12-31 08:31
浏览 20
已采纳

如何计算这个对象数组? [重复]

This question already has an answer here:

So i've been trying to count this object array for ages, and nothing seem to work for me...

stdClass Object
(
    [type] => champion
    [version] => 5.24.2
    [data] => stdClass Object
        (
            [Thresh] => stdClass Object
                (
                    [id] => 412
                    [key] => Thresh
                    [name] => Thresh
                    [title] => the Chain Warden
                    [info] => stdClass Object
                        (
                            [attack] => 5
                            [defense] => 6
                            [magic] => 6
                            [difficulty] => 7
                        )

                )

            [Aatrox] => stdClass Object
                (
                    [id] => 266
                    [key] => Aatrox
                    [name] => Aatrox
                    [title] => the Darkin Blade
                    [info] => stdClass Object
                        (
                            [attack] => 8
                            [defense] => 4
                            [magic] => 3
                            [difficulty] => 4
                        )

                )

            [Tryndamere] => stdClass Object
                (
                    [id] => 23
                    [key] => Tryndamere
                    [name] => Tryndamere
                    [title] => the Barbarian King
                    [info] => stdClass Object
                        (
                            [attack] => 10
                            [defense] => 5
                            [magic] => 2
                            [difficulty] => 5
                        )

                )

            [Ezreal] => stdClass Object
                (
                    [id] => 81
                    [key] => Ezreal
                    [name] => Ezreal
                    [title] => the Prodigal Explorer
                    [info] => stdClass Object
                        (
                            [attack] => 7
                            [defense] => 2
                            [magic] => 6
                            [difficulty] => 7
                        )

                )

        )

)

What i want to count is the length of data.

Plot Twist: count($array->data) doesn't work.

If it counts right, it should return 3. Thank you in advance!

</div>
  • 写回答

4条回答 默认 最新

  • dtxq82489 2015-12-31 08:38
    关注

    In your case data is an object. Cast it to array:

     $count = count((array) $array->data);
    

    the other way is using get_object_vars

    $count = count(get_object_vars($array->data));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?