如何动态引用 php ARRAY 变量?

发布于 2024-12-11 18:02:35 字数 289 浏览 0 评论 0原文

大家都知道可以使用 ${'varName'} 访问 PHP 中的变量。但是,当您需要获取/设置一个属于数组一部分的变量时,为什么它不起作用?假设我们有这段代码:

<?php
   $myArray = array(...);
   $myVarName = "myArray['my1']['my11']['my111']";
   ${$myVarName} = "new value";
?>

它不应该工作吗? 我已经一次又一次地测试过它 - 它不起作用.. 有办法做到这一点吗?

Everybody knows that you can access a variable in PHP using this: ${'varName'}. But when you need to get/set a variable witch is part of an array, why doesn't it work ? Suppose we have this piece of code:

<?php
   $myArray = array(...);
   $myVarName = "myArray['my1']['my11']['my111']";
   ${$myVarName} = "new value";
?>

Shouldn't it work ?
I have tested it again and again - it is not working..
Is it there a way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无言温柔 2024-12-18 18:02:35

我建议您不要使用像 ${$var} 这样的动态变量。
您想要的是根据键的路径修改多维关联数组。

<?php
$myArray = array(...); // multi-dimensional array
$myVarPath = array('my1', 'my11', 'my111');
setValueFromPath($myArray, $myVarPath);

function getValueFromPath($arr, $path)
{
    // todo: add checks on $path
    $dest = $arr;
    $finalKey = array_pop($path);
    foreach ($path as $key) {
        $dest = $dest[$key];
    }
    return $dest[$finalKey];
}

function setValueFromPath(&$arr, $path, $value)
{
    // we need references as we will modify the first parameter
    $dest = &$arr;
    $finalKey = array_pop($path);
    foreach ($path as $key) {
        $dest = &$dest[$key];
    }
    $dest[$finalKey] = $value;
}

为了简单起见,这是一个程序示例。您可能希望将分层​​数组和此函数放入一个类中。

I recommend you not to use dynamic variables like ${$var}.
What you want is modifying a multi-dimensional associative array according to a path of keys.

<?php
$myArray = array(...); // multi-dimensional array
$myVarPath = array('my1', 'my11', 'my111');
setValueFromPath($myArray, $myVarPath);

function getValueFromPath($arr, $path)
{
    // todo: add checks on $path
    $dest = $arr;
    $finalKey = array_pop($path);
    foreach ($path as $key) {
        $dest = $dest[$key];
    }
    return $dest[$finalKey];
}

function setValueFromPath(&$arr, $path, $value)
{
    // we need references as we will modify the first parameter
    $dest = &$arr;
    $finalKey = array_pop($path);
    foreach ($path as $key) {
        $dest = &$dest[$key];
    }
    $dest[$finalKey] = $value;
}

This is a procedural example to keep it simple. You may want to put your hierarchical array and this functions inside a class.

瀟灑尐姊 2024-12-18 18:02:35

这不应该起作用吗?

不。

每个人都知道可以使用 ${'varName'} 访问 PHP 中的变量。

是的。但每个人都知道这是蹩脚的。

如何动态引用 php 数组变量?

拥有 ('my1','my11','my111') 数组,您可以仅使用循环来引用任何特定的数组成员。

Shouldn't it work ?

No.

Everybody knows that you can access a variable in PHP using this: ${'varName'}.

Yes. Yet everybody knows that's lame.

How to refer dynamically to a php array variable(s)?

having array of ('my1','my11','my111') you can refer to any particular array member using merely a loop.

掩饰不了的爱 2024-12-18 18:02:35

可以做这样的事情,但这将是一个非常糟糕的主意。对不起上校;)

<?php
$mA = array();
$mVN = "mA['m1']['m2']['m3']";
eval('
. $mVN . ' = "new value";');
print_r($mA);
?>

You could do something like this, but it would be a really bad idea. Sorry Col. ;)

<?php
$mA = array();
$mVN = "mA['m1']['m2']['m3']";
eval('
. $mVN . ' = "new value";');
print_r($mA);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文