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

1

3 Answers 3

2

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

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

PHP: Count an stdClass object

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.