-1

If I do this:

$array = ["one", "two", "three"];

$array_copy = "$array"; // or "{$array}"

I doesn't get the original array but instead a string conversion of it. ¿Is there any way to acomplish this task? To get the array reference by his string name.

Thank you.

Edit:

I am aware of this:

$array = ["one", "two", "three"];

$array_copy = $"array"; 

or

$name = "array";

$array_copy = $$name

But I need to achive this in any situation. Example:

$array = ["one", "two", "three" => ["four"] ];

$sub_array = "{$array['three']}"; // this returns an string, not the array ["four"]

I hope is more clear now.

Edit 2

Let's put it in other way. Imagine you need that an user input (string) be able to access the content of any declared variable. Example:

$customer = [ "name"=>"Peter", "address"=> ["street"=>"5th", "number"=>1969] ];

$variable_name = $_GET["varname"];

var_export( $$variable_name ); // What can write the user to print $customer["address"] array?

Community
  • 1
  • 1
Jorge Luis
  • 31
  • 7
  • Are you looking for `${'array'}`? See [variable variables](https://www.php.net/manual/en/language.variables.variable.php). – Jeto Dec 10 '19 at 21:48
  • `${'array'}['three']` after your edit then... Did you read the link? – Jeto Dec 10 '19 at 22:07
  • Can you give an example of the scenario where this is necessary? Seems like there might be a better way. – Don't Panic Dec 10 '19 at 22:22
  • Hello @Jeto ${'array'}['three'] is not working for me cause I have no control of the syntax, it can be ${'array'}['three'] or ${'array'}['three']['two'] or anything. For this reason a need it to be all string. Thanks for your time – Jorge Luis Dec 11 '19 at 01:39
  • @Don't Panic its a templating situation, I need to pass a variable name to a child template. Later, this name has to be parsed in the child, for accessing the data contained in the parent variable (for example as an argument for a "foreach loop"). Its a bit confusing but maybe you're right, I need to think it more and do it in another way. – Jorge Luis Dec 11 '19 at 01:46
  • I added a new better example in Edit 2 – Jorge Luis Dec 11 '19 at 02:01
  • 1
    Does this answer your question? [How to access and manipulate multi-dimensional array by key names / path?](https://stackoverflow.com/questions/27929875/how-to-access-and-manipulate-multi-dimensional-array-by-key-names-path) – Jeto Dec 11 '19 at 06:29
  • @Jeto unfortunately not. I use this technique you suggested for other purposes, What I need in this case is the answer to Edit 2. Thanks again. – Jorge Luis Dec 11 '19 at 17:52

2 Answers2

1

because you equal to string, just do it directly

$array_copy = $array

but copy is just copy, not a referense, if you want reference you should write like this

$array_copy = &$array

but there are should be reasons to get the reference

or if you have some variable with array name then you can do like this

$array = ["one", "two", "three"];
$arrayName = 'array';

$array_copy = $$arrayName;
Dmitry Reutov
  • 2,995
  • 1
  • 5
  • 20
0

You may use a function that takes a path such as customer.address as a parameter to retrieve the address index of the $customer array automatically:

$customer = ['name' => 'Peter', 'address' => ['street' => '5th', 'number' => 1969]];

/**
 * @param array $array
 * @param string $path A dot-separated property path.
 * @param mixed $default
 * @return mixed
 */
function getArrayValue(array $array, string $path, $default = null)
{
  $parts = explode('.', $path);
  return array_reduce($parts, static function ($value, $part) use ($default) {
    return $value[$part] ?? $default;
  }, $array);
}

/**
 * @param string $path A dot-separated path, whose first part is a var name available in the global scope.
 * @param mixed $default
 * @return mixed
 */
function getGlobalArrayValue(string $path, $default = null)
{
  @list($varName, $propertyPath) = explode('.', $path, 2);
  return getArrayValue($GLOBALS[$varName] ?? [], $propertyPath, $default);
}

echo getGlobalArrayValue('customer.name'), PHP_EOL; // Peter
echo getGlobalArrayValue('customer.address.street'), PHP_EOL; // '5th'
echo getGlobalArrayValue('customer.address.idontexist', 'somedefaultvalue'), PHP_EOL; // 'somedefaultvalue'
echo getGlobalArrayValue('idontexist.address', 12); // 12

Demo: https://3v4l.org/O6h2P

Jeto
  • 14,596
  • 2
  • 32
  • 46