1

I'm still new at PHP and I can't seem to count the number of Objects within another object. The stdClass object looks like this:

stdClass Object (

[data] => Array (
    [0] => stdClass Object (
        [Code] => ABC
        [Title] => Alphabet
        [sections] => Array (
            [0] => stdClass Object (
                [Name] => Sounds
                [sections] => Vowels
            )
        )
    )

)

I must count the number of elements in this object so i can echo it properly. For the data, I was able to do it:

$number = count($hanap->data);

I don't know how to do it for the sections.

$number = count($hanap->data->sections); // does not work.

Thanks. Any help will be greatly appreciated. :)

alex
  • 438,662
  • 188
  • 837
  • 957
Joshua
  • 63
  • 3
  • 8
  • $total = count((array)$obj); http://stackoverflow.com/questions/1314745/php-count-an-stdclass-object – yousef Apr 05 '17 at 10:26

3 Answers3

2

this will solve your problem, just cast the object to array and count it

$total = count((array)$obj);

PHP: Count an stdClass object

Community
  • 1
  • 1
yousef
  • 864
  • 9
  • 13
1
count($hanap->data[0]->sections)
deceze
  • 471,072
  • 76
  • 664
  • 811
1

You are missing the first member of the array where they are...

$number = count($hanap->data[0]->sections)
alex
  • 438,662
  • 188
  • 837
  • 957