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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:30:18+00:00 2026-05-12T07:30:18+00:00

I have an array in php like this: $myArray = array(‘name’=>’juank’, ‘age’=>26, ‘config’=>array(‘usertype’=>’admin’,’etc’=>’bla bla’));

  • 0

I have an array in php like this:

$myArray = array(‘name’=>’juank’, ‘age’=>26, ‘config’=>array(‘usertype’=>’admin’,’etc’=>’bla bla’));

I need this array to be accesible along the script to allow changes in any field EXCEPT in the “config” field. Is there a way to protect an array or part of an array from being modified as if it where declared private inside a class? I tried defining it as a constant but it’s value changes during script execution. Implementing it as a class would mean I’d have to rebuild the complete application from scratch :S

thanks!

  • 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-12T07:30:19+00:00Added an answer on May 12, 2026 at 7:30 am

    I do not think you can do this using “pure” “real” arrays.

    One way to get to this might be using some class that implements ArrayInterface ; you code would look like it’s using arrays… But it would actually be using objects, with accessor methods that could forbid write-access to some data, I guess…

    It would have you change a couple of things (creating a class, instanciating it) ; but not everything : access would still be using an array-like syntax.

    Something like this might do the trick (adapted from the manual) :

    class obj implements arrayaccess {
        private $container = array();
        public function __construct() {
            $this->container = array(
                "one"   => 1,
                "two"   => 2,
                "three" => 3,
            );
        }
        public function offsetSet($offset, $value) {
            if ($offset == 'one') {
                throw new Exception('not allowed : ' . $offset);
            }
            $this->container[$offset] = $value;
        }
        public function offsetExists($offset) {
            return isset($this->container[$offset]);
        }
        public function offsetUnset($offset) {
            unset($this->container[$offset]);
        }
        public function offsetGet($offset) {
            return isset($this->container[$offset]) ? $this->container[$offset] : null;
        }
    }
    
    
    $a = new obj();
    
    $a['two'] = 'glop'; // OK
    var_dump($a['two']); // string 'glop' (length=4)
    
    $a['one'] = 'boum'; // Exception: not allowed : one
    

    You have to instanciate an object with new, which is not very array-like… But, after that, you can use it as an array.

    And when trying to write to an “locked” property, you can throw an Exception, or something like that — btw, declaring a new Exception class, like ForbiddenWriteException, would be better : would allow to catch those specifically 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 225k
  • Answers 225k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Something like this might work: var random = new Random();… May 13, 2026 at 1:05 am
  • Editorial Team
    Editorial Team added an answer One solution would be for you to provide the user… May 13, 2026 at 1:05 am
  • Editorial Team
    Editorial Team added an answer So how does one master DOM? This is almost as… May 13, 2026 at 1:05 am

Related Questions

I have an array in php like this: $myArray = array('name'=>'juank', 'age'=>26, 'config'=>array('usertype'=>'admin','etc'=>'bla bla'));
Say I have an array of key/value pairs in PHP: array( 'foo' => 'bar',
Hello! I'm not even sure if this is possible, but hopefully it is in
In PHP I have an array containing 20 elements or more. The keys have
I have a class in PHP like so: class ParentClass { public function __construct($arg)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.