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

  • SEARCH
  • Home
  • 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 7044027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:21:38+00:00 2026-05-28T02:21:38+00:00

For a SOAP service I have to generate an object, which can have an

  • 0

For a SOAP service I have to generate an object, which can have an arbitrary number of nested objects of the same type. The only working solution I have come up with was one using eval. I have simplified the code somewhat, in reality the objects in the $nestedObjArray are considerably larger.

$nestedObjArray = array();
$nestedObjArray[] = new stdClass();
$nestedObjArray[] = new stdClass();
$nestedObjArray[] = new stdClass();

$finalObj = new stdClass();
for ($i = 0; $i < count($nestedObjArray); $i++) {
    $nestedStr = str_repeat("->nested", $i);
    eval('$finalObj->nested'.$nestedStr.' = $nestedObjArray[$i];');
}

Which generates the following 3 statements:

$finalObj->nested = $nestedObjArray[0];
$finalObj->nested->nested = $nestedObjArray[1];
$finalObj->nested->nested->nested = $nestedObjArray[2];

This works fine, but is pretty ugly. Can anyone think of a more elegant solution? Btw, the following instead of the eval line doesn’t work:

$finalObj->nested{$nestedStr} = $nestedObjArray[$i];
  • 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-05-28T02:21:39+00:00Added an answer on May 28, 2026 at 2:21 am

    What you really should do is keep a separate variable that points to the inner object. For instance…

    $finalObj = new stdClass();
    $innerObj = $finalObj;
    for($i = 0; $i < count($nestedObjArray); $i++) {
        $innerObj->nested = $nestedObjArray[$i];
        $innerObj = $innerObj->nested;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can i implement a SOAP Service which can deal with delegates/events?? Can i also
I have a SOAP service that returns an array of a complex type. The
Can any body have idea how to consume php soap web service that have
I have a SOAP service, the request and responses work as expected with good
I Have an internal SOAP Web service that is being called from an external
I have a complex RIA client that communicates with a WCF SOAP web service,
we have a public asmx service that is accessed by our clients through SOAP
I have to use several SOAP messages to get data from a web service.
I have an ASP.NET web service running that accepts both HTTP POST and SOAP
I'm working on a project where we have several SOAP Web Services developed on

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.