Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9085253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:12:03+00:00 2026-06-16T21:12:03+00:00

Possible Duplicate: Iterating through stdClass obj’s that are contained within a parent stdClass Obj.

  • 0

Possible Duplicate:
Iterating through stdClass obj’s that are contained within a parent stdClass Obj. (aka: iterating through child objects)

…bccause if the parent object is empty foreach shows an error. for() would be my preferred loop logic but I do not know how to access the child objects properties.

Work-ish code:

foreach ($main_object as $object) {
    $object->value = $object->value * 3.14;
}

BUT when $main_object is empty or null, foreach dumps out an error.

I’m looking for something more like this:

(pseudo) code:

for ($p = 0; $p < counter($main_object); $p++) {
    //$p being the index'd id of the child object. That is where I'm having issues, how do I refer to child X within a parent object.
    $main_object->$p->value = $main_object->$p->value * 3.14;
}

Now if ‘$main_object’ is empty/null no error is thrown, logic keeps on truckin’.

How do I access child object $x->properties when object is one of a group of child objects inside a parent object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:12:04+00:00Added an answer on June 16, 2026 at 9:12 pm

    Just check first that the object contains at least one item before iterating:

    if($a && count($a)){
        // iterate using foreach
    }
    

    I too find this annoying, but foreach is extremely easy to work with so it’s a bit of a tradeoff. I’d still rather do the above than use something like for (which could also work).

    PHP count docs

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Iterating through a range of dates in Python I have two entries:
Possible Duplicate: Iterating through a String and replacing single chars with substrings in haskell
Possible Duplicate: for vs foreach vs while which is faster for iterating through arrays
Possible Duplicate: C# Iterating through an enum? (Indexing a System.Array) All I want to
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Possible Duplicate: Iterating through a LinkedHashMap in reverse order How to traverse Linked Hash
Possible Duplicate: Loop through Json object { data: [ { name: Jen, id: 1
Possible Duplicate: mysql count into PHP variable I have the following query that returns
Possible Duplicate: What's the difference between iterating over a file with foreach or while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.