PHP variable variables with array key

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

So, I have a section of code like this. I have a string with a value containing square brackets similar to those used when accessing the array key. I want to create this very array of keys using the value of the strings. I hope this code understands a bit better 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'. 

It does not seem to work correctly. Is it possible (or how else can I do this)?

+6
source share
5 answers

The following is an example following the syntax of a variable name, which also resolves array elements:

 // 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; }; }; 

Since you cannot overload variables in PHP, you are limited to expressing PHP here, that is, there is no write context for variable references as function return values, which requires additional smoothing:

 $var = &$vars($string); 

If something like this does not fit your needs, you need to fix PHP, and if not an option, PHP does not offer the language features you are looking for.

See also a related question: use strings to access (potentially large) multidimensional arrays .

+5
source

The variable variables are characters in a string processed literally. They do not interpret what is inside the string. Thus, the characters [] in $string not considered as an array entry. Rather, it is considered as part of the variable name.

after executing $$string = 'thisworks'; PHP creates a variable called welcome["hello"] literally with the value set to thisworks .

Use this notation for printing.

 print (${'welcome["hello"]'}); 
+5
source

No, you cannot do that. Alternatively, you can set the reference variable using

 $welcomeHello = &$welcome["hello"] 

and then use

 $welcomeHello = 'thisworks' 

to customize the content of the item.

This will be a more correct and even faster way to set array elements.

+1
source

You can use eval, although I would not recommend it in a production environment, it has its advantages.

 <? $string = 'welcome["hello"]'; eval('$' . $string . " = 'thisworks';"); // This also works : eval("$$string = 'thisworks';"); // I could then print the array keys value like this print( $welcome["hello"] ); // Prints 'thisworks' 
0
source

You can use two variables, one to store the name of the specified link, and the other to store the key:

 <?php $myArray = array('foo', 'bar', 'baz'); $varName = 'myArray'; $varKey = 1; echo ${$varName}[$varKey]; // will echo "bar" 

According to why braces for dereferencing $varName :

To use variable variables with arrays, you must solve the ambiguity problem. That is, if you write $$a[1] , then the parser should know if you wanted to use $a[1] as a variable or if you wanted $$a as a variable, and then index [1] from this variable . The syntax for resolving this ambiguity is ${$a[1]} for the first case and ${$a}[1] for the second.

(from https://www.php.net/manual/en/language.variables.variable.php )

0
source

Source: https://habr.com/ru/post/914992/


All Articles