Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Variable Variables with array key

Tags:

php

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)?

like image 535
Matthew Ruddy Avatar asked May 05 '12 23:05

Matthew Ruddy


3 Answers

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.

like image 190
hakre Avatar answered Nov 17 '22 02:11

hakre


In Variable variables characters in a string treated literally. They do not interpret whats inside the string. So [] characters in the $string is not treated as array notation. Rather its treated as a part of the variable name.

after you execute $$string = 'thisworks'; PHP creates a variable with name welcome["hello"] literally with value set to thisworks.

To print it use this notation,

print (${'welcome["hello"]'});
like image 31
Shiplu Mokaddim Avatar answered Nov 17 '22 03:11

Shiplu Mokaddim


No, you can't do that in such way. Alternatively, you can set reference variable using

$welcomeHello = &$welcome["hello"]

and then use

$welcomeHello = 'thisworks'

for setting item content.

This would be more correct and even faster way for setting array items.

like image 1
RReverser Avatar answered Nov 17 '22 02:11

RReverser