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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:15:24+00:00 2026-06-03T12:15:24+00:00

Just wondering if there is a way around this (or if it is even

  • 0

Just wondering if there is a way around this (or if it is even possible).

So I have a section of code similar to this. I have a string, with a value that contains square brackets, similar to those used when accessing an array key. I want to create that array key itself by using the strings value. I hope this code makes a little more sense of what I mean.

// String that has a value similar to an array key
$string = 'welcome["hello"]';
$$string = 'thisworks';

// I could then print the array keys value like this
print( $welcome["hello"] );

// This would hopefully output 'thisworks'.

Can’t seem to get it to work correctly, however. Is it possible (or how else can I go about it)?

  • 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-03T12:15:25+00:00Added an answer on June 3, 2026 at 12:15 pm

    The following is an example following your variable name syntax that resolves array members as well:

    // String that has a value similar to an array key
    $string = 'welcome["hello"]';
    
    // initialize variable function (here on global variables store)
    $vars = $varFunc($GLOBALS);
    
    // alias variable specified by $string
    $var = &$vars($string);
    
    // set the variable
    $var = 'World';
    
    // show the variable
    var_dump($welcome["hello"]); # string(5) "World"
    

    With the following implementation:

    /**
     * @return closure
     */
    $varFunc = function (array &$store) {
        return function &($name) use (&$store)
        {
            $keys = preg_split('~("])?(\\["|$)~', $name, -1, PREG_SPLIT_NO_EMPTY);
            $var = &$store;
            foreach($keys as $key)
            {
                if (!is_array($var) || !array_key_exists($key, $var)) {
                    $var[$key] = NULL;
                }
                $var = &$var[$key];
            }
            return $var;
        };
    };
    

    As you can not overload variables in PHP, you are limited to the expressiveness of PHP here, meaning there is no write context for variable references as function return values, which requires the additional aliasing:

    $var = &$vars($string);
    

    If something like this does not suit your needs, you need to patch PHP and if not an option, PHP is not offering the language features you’re looking for.

    See as well the related question: use strings to access (potentially large) multidimensional arrays.

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

Sidebar

Related Questions

just wondering if there is a way to reduce the amount of code needed
Just wondering if there is a way in Spring to have a parent controller:
Cascading deletes aren't setup on this particular database. Just wondering if there's a way
I was just wondering if there was a possible way to add a custom
Wondering if this is even possible or a limitation of PHP, googling around seems
Just wondering is there any way I can check whether the url links to
Just wondering if there is any way to get the NS records in C#.
I'm just wondering if there is a way to set a vertical threshold of
just was wondering if there is a way to access a method from my
I'm just wondering, is there any way to run Selenium tests on Android in

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.