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!

131
  • 1,285
  • 1
  • 12
  • 30

4 Answers4

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));
RomanPerekhrest
  • 73,078
  • 4
  • 37
  • 76
  • Thank you! works perfect. Couldn't seem to find this no matter how much i googled... – 131 Dec 31 '15 at 08:45
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.

Hassan Althaf
  • 1,211
  • 1
  • 16
  • 31
1

Used below code:

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

Instead of:

count($array->data);
hardik solanki
  • 2,867
  • 1
  • 15
  • 22
1

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

Ali Zia
  • 3,539
  • 3
  • 23
  • 63