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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:24:20+00:00 2026-05-30T18:24:20+00:00

I have a multidimensional array with various config settings. Here’s an example: $this->data =

  • 0

I have a multidimensional array with various config settings. Here’s an example:

$this->data = array();

I want this array to be private so I’ve created a getter:

public function getData(){
    $args = func_get_args();
    $x = $this->data;
    foreach($args as $arg) $x = $x[$arg];
    return $x;
}

and I use it like this:

$value = $obj->getData('country', 'city', 'street');

Everything works fine but the problem is that it is 5x slower (tested with 100,000 iterations) than direct access:

$value = $obj->data['country']['city']['street'];

What is the best way to do this? Is there any way to make this variable read-only or is there a better way to access it without using foreach()?

  • 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-30T18:24:22+00:00Added an answer on May 30, 2026 at 6:24 pm

    You can create getter with __get magic method:

    public function __get($name) {
        return $this->$name;
    }
    

    In such way you can get protected & private properties from anywhere in the same way but set them only inside your class.

    If you want only this property to behave in such way, simply use condition:

    public function __get($name) {
        return $name === 'data' ? $this->data : null;
    }
    

    Of course, __get is even a bit slower then method getter, but it needs to be called only once for access to data property, and every access to any sub-array of it will be as fast as direct access (that means no ugly foreach iterations 🙂 ).

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

Sidebar

Related Questions

I have a multidimensional array, here: $noticeDate = json_encode( $noticesDates ); and I want
I have a multidimensional array and want to break it down. Here's the array.
I have a multidimensional array converted to JSON data in this format. [[null,null,null,null,null,null],[null,null,null,1,1,null],[null,null,null,null,1,1],[null,null,null,null,null,null],[null,null,null,null,null,null]] I
say I have a multidimensional array like this: $data[0]['country'] = Angola $data[0]['report'] = Food
hi i have multidimensional array in php i want to remove an index from
I have an multidimensional array that looks like this: Array ( [0] => Array
Explanation I have a multidimensional array that is iterated over to created a categorized
I have a large multidimensional array and I basically want to drop the first
For example, I have multidimensional array as below: $array = array ( 0 =>
I have a multidimensional array that looks something like this: ourThing = array( 'id'

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.