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 8630185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:00:11+00:00 2026-06-12T09:00:11+00:00

I ceated a function to convert a list of parent child to an object

  • 0

I ceated a function to convert a list of parent child to an object of class “city” :

   public static function createCity(array $config)
{
    $city= new City();

    $id=key($config);
    $label= $config[$id]['label'];

    $city->setId($id);
    $city->setLabel($label);

    $children = $config[$id]['childrens'];

    if(!empty($children)){
        foreach($children as $key_child => $child) {
            $children = array($key_child => $child);

            $city->addChild(self::createCity($children));

        }
    }
  return $city;
}

Now I would to creat a function to do the opposite => convert an object of type Class City to an array,so I do like that :

   public function getCityArray(City$rootCity)
{
        $result = array();

        $result['id'] = $rootCity->getId();
        $result['label']= $rootCity->getLabel();

    $children = $rootCity->getChildren();


    if ( !empty($children)) {
        foreach ($children as $key_child => $child) {

            $result['childrens'] = array($key_child => $child );

            $result[] = $this->getCityArray($child);
        }
    }

    return $result;

}

But it doesn’t work because when I do var_dump(‘$result’) so I have a list with no end and the loop does not stop?

  • 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-12T09:00:14+00:00Added an answer on June 12, 2026 at 9:00 am

    Try this. Since I don’t know have the full code, not sure if it will work. The class variable $result will contain the results.

    $result = array();
    public function getCityArray(City $rootCity) {
    
      $result['id'] = $rootCity->getId();
      $result['label']= $rootCity->getLabel();
      $children = $rootCity->getChildren();
    
      if ( !empty($children)) {
        $result['childrens'] = $children;
        $this->result[] = $result;
        foreach ($children as $key_child => $child) {
          $this->getCityArray($child);
        }
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created the following within my class private List<CreditCardTransaction> _ccTransactions = new List<CreditCardTransaction>(); public
I have been looking around for some way to convert a function or class
in my php5 code i want to use the UNIX_TIMESTAMP function to convert a
I want create a function that verify if the numbers of a list fall
//ARC is turned on in Xcode 4.2 A static function is created which executes
Hi I'm trying to convert packet capturing in Example3 in SharpPcap new version SharpPcap-2.2.0rc1.src
I have created a function which will convert any string into tab delimited. What's
So I have these two functions, public static string[] CharToHex(string str, string prefix, string
I have created a javascript sort function to sort a list of articles by
I've created a function to convert a number into a roman numeral. I know

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.