문제

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

도움이 되었습니까?

해결책

count($hanap->data[0]->sections)

다른 팁

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

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

PHP: Count an stdClass object

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

$number = count($hanap->data[0]->sections)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top