0

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!

5
  • 3
    This is the same as: stackoverflow.com/questions/1314745/… Commented Dec 31, 2015 at 8:38
  • Wait what? Are these League of Legends Champions? :D Commented Dec 31, 2015 at 8:40
  • Haha awesome :D Which server do you play on? I play on EUW :D fizz main :D Commented Dec 31, 2015 at 8:42
  • Lol. I think we should keep league out of Stack Overflow xD But i play on EUW. Name: whattowritehere Commented Dec 31, 2015 at 8:43
  • add me on gmail [email protected] and summoner's name "Syed Ali Zia" => Silver 5 Commented Dec 31, 2015 at 8:44

4 Answers 4

2

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! works perfect. Couldn't seem to find this no matter how much i googled...
1

Not that hard, you can do this:

$object = [ .... ]; // The object you show above.

function countElements($object) {
    return count(get_object_vars($object));
}

$count = countElements($object);

That should get you the answer.

get_object_vars() documentation.

Comments

1

Used below code:

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

Instead of:

count($array->data);

Comments

1

Simply use count((array) $array->data);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.