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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:59:53+00:00 2026-06-16T09:59:53+00:00

I need to create a nested structure class of a multidimensional array, this is

  • 0

I need to create a nested structure class of a multidimensional array, this is my array:

Array
(

    [days] => Array
        (
            [0] => Array
                (
                    [rows] => Array
                        (
                            [0] => Array
                                (
                                    [activity_id] => 1
                                    [name] => Activity 2
                                    [city] => London
                                    [info] => fsdsdshgsfd 
                                )

                            [1] => Array
                                (
                                    [activity_id] => 3
                                    [name] => Activity 1
                                    [city] => London
                                    [info] => fsdhgsfd 
                                )
                        )

                )
           [1] => Array
                (
                    [rows] => Array
                        (
                            [0] => Array
                                (
                                    [activity_id] => 3
                                    [name] => Activity 1
                                    [city] => London
                                    [info] => fsdhgsfd 
                                )

                                ...

                                )
                        )
                )

                ...
       )
)

I have been trying to rewrite my code to make it class-driven, but I am struggling with that, what is the right way to build a class structure Itinerary->Days->Rows to replace an array? I tried something like this, I am not sure if it makes sense, I don’t really understand the way how it has to be done:

class Itinerary
{

    private $days = array();

    public static function addDay($day) {
        $this->$days[] = new ItineraryDay($day);
    }
}

class ItineraryDay implements Countable
{

    private $rows = array();

    public static function addRow($row) {
        $this->$rows[] = new ItineraryRow($row);
    }

    public function count()
    {
        return count($this->rows);
    }
}

class ItineraryRow implements Countable
{

    private $name;
    private $city;
    ...

    function __get($key)
    {
        ...
    }

    function __set($key, $value)
    {
        ...
    }

    public function count()
    {
        return count($this->rows);
    }
}


$itinerary1 = new Itinerary(); 

$day1 = new ItineraryDay(); 
$itinerary1->addDay($day1);

$row1 = new ItineraryRow(); 
$day1->addRow($row1);

Can someone guide me?

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

    It really depends what you ultimately want to do with said structure, but for a general idea I typically do something like this:

    class Itinerary implements Countable
    {
        private $days;
    
        public function __construct( array $days = array() )
        {
            $this->setDays( $days );
        }
    
        public function addDay( ItineraryDay $day )
        {
            $this->days[] = $day;
        }
    
        public function setDays( array $days )
        {
            $this->days = array();
            foreach( $days as $day )
            {
                $this->addDay( $day );
            }
        }
    
        public function count()
        {
            return count( $this->days );
        }
    }
    
    class ItineraryDay implements Countable
    {
        private $rows;
    
        public function __construct( array $rows = array() )
        {
            $this->setRows( $rows );
        }
    
        public function addRow( ItineraryRow $row )
        {
            $this->rows[] = $row;
        }
    
        public function setRows( array $rows )
        {
            $this->rows = array();
            foreach( $rows as $row )
            {
                $this->addRow( $row );
            }
        }
    
        public function count()
        {
            return count( $this->rows );
        }
    }
    
    class ItineraryRow
    {
        private $id;
        private $name;
        private $city;
        private $info;
    
        public function __construct( $id, $name, $city, $info )
        {
            $this->id   = $id;
            $this->name = $name;
            $this->city = $city;
            $this->info = $info;
        }
    
        /* ... */
    }
    

    Then using it with the structure of your current array of data:

    $days = array();
    foreach( $originalData[ 'days' ] as $days )
    {
        $rows = array();
        foreach( $days[ 'rows' ] as $row )
        {
            $rows[] = new ItineraryRow( $row[ 'activity_id' ], $row[ 'name' ], $row[ 'city' ], $row[ 'info' ] );
        }
        $days[] = new ItineraryDay( $rows );
    }
    $itinerary = new Itinerary( $days );
    

    Or:

    $itinerary = new Itinerary;
    foreach( $originalData[ 'days' ] as $days )
    {
        $day = new ItineraryDay;
        foreach( $days[ 'rows' ] as $row )
        {
            $row = new ItineraryRow( $row[ 'activity_id' ], $row[ 'name' ], $row[ 'city' ], $row[ 'info' ] )
            $day->addRow( $row );
        }
        $itinerary->addDay( $day );
    }
    

    So, you can either pass “child” objects to the constructor (the method that constructs a new object), or add them with methods after construction. If you want the objects to be immutable, meaning you don’t want to allow the objects to accept any more rows / days after construction, just make the addDay, setDays, addRow and setRows methods protected or private thereby only allowing passing “child” object through the constructors.

    Be aware that, as PeeHaa already mentioned, you don’t want static methods, because they operate class wide, not on individual instances of classes (objects). As a matter of fact, you cannot even use static methods the way you intended, because $this is only available in object context, not in class wide context.

    But, to be honest, the question is a little bit to vague to be answered properly. We’d have to have a little more details about how you are going to construct the objects, and how you are going to use them later on.

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

Sidebar

Related Questions

Using boost python I need create nested namespace. Assume I have following cpp class
I need to create a large matrix (array) structure (3 axis) and each element
If I need to create a couple of nested DOM elements, I know one
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
Need to create a custom DNS name server using C which will check against
I have been stumped by this problem. I need to create a pattern such
I need to create N nested loops to print all combinations of a binary
I need to create this function flat that's supposed to re-contract a new list
I need to create and then pass an array/hash from Rails into Javascript through
So My problem is: I want to create nested array from string as reference.

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.