如何动态访问可变多维数组中的值

发布于 2024-12-05 08:55:42 字数 817 浏览 0 评论 0原文

$first = array("a", "b" => array("c", "d" => array("e", "f")), "g", "h" => array("f"));
$second = array("b", "d", "f");
$string = "foobar";

给定上面的代码,如何将 $first 中定义于 $second 的索引处的值设置为 $string 的内容?意思是,对于这个例子,它应该是 $first["b"]["d"]["f"] = $string;,但是 $second$first 可以是任意长度。然而,$second 始终是一维的。这是我尝试过的方法,它似乎没有按计划工作:

$key = "";
$ptr = $first;
for($i = 0; $i < count($second); $i++)
{
    $ptr &= $ptr[$second[$i]];
    $key = key($ptr);
}
$first[$key] = $string;

这将执行 $first["f"] = $string; 而不是正确的多维索引。我原以为使用 key 会找到数组中的位置,包括它已经向下移动的级别。

如何动态访问正确的密钥?如果维度数是静态的,我可以解决这个问题。

编辑:另外,我想要一种不使用 eval 的方法。

$first = array("a", "b" => array("c", "d" => array("e", "f")), "g", "h" => array("f"));
$second = array("b", "d", "f");
$string = "foobar";

Given the above code, how can I set a value in $first at the indexes defined in $second to the contents of $string? Meaning, for this example, it should be $first["b"]["d"]["f"] = $string;, but the contents of $second and $first can be of any length. $second will always be one dimensional however. Here's what I had tried, which didn't seem to work as planned:

$key = "";
$ptr = $first;
for($i = 0; $i < count($second); $i++)
{
    $ptr &= $ptr[$second[$i]];
    $key = key($ptr);
}
$first[$key] = $string;

This will do $first["f"] = $string; instead of the proper multidimensional indexes. I had thought that using key would find the location within the array including the levels it had already moved down.

How can I access the proper keys dynamically? I could manage this if the number of dimensions were static.

EDIT: Also, I'd like a way to do this which does not use eval.

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

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

发布评论

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

评论(1

无声情话 2024-12-12 08:55:42

实际情况比这要复杂一些。如果每个级别尚不存在,则必须对其进行初始化。但您的实际问题是:

  • 您想要添加值的数组位于 $ptr 中,而不是位于 $first 中。
  • $x &= $y$x = $x & 的简写形式。 $y(按位与)。您想要的是 x = &$y (通过引用分配)。

这应该可以做到:

function assign(&$array, $keys, $value) {
    $last_key = array_pop($keys);
    $tmp = &$array;
    foreach($keys as $key) {
        if(!isset($tmp[$key]) || !is_array($tmp[$key])) {
            $tmp[$key] = array();
        }
        $tmp = &$tmp[$key];
    }
    $tmp[$last_key] = $value;
    unset($tmp);
}

用法:

assign($first, $second, $string);

DEMO

It's a bit more complicated than that. You have to initialize every level if it does not exist yet. But your actual problems are:

  • The array you want to add the value to is in $ptr, not in $first.
  • $x &= $y is shorthand for $x = $x & $y (bitwise AND). What you want is x = &$y (assign by reference).

This should do it:

function assign(&$array, $keys, $value) {
    $last_key = array_pop($keys);
    $tmp = &$array;
    foreach($keys as $key) {
        if(!isset($tmp[$key]) || !is_array($tmp[$key])) {
            $tmp[$key] = array();
        }
        $tmp = &$tmp[$key];
    }
    $tmp[$last_key] = $value;
    unset($tmp);
}

Usage:

assign($first, $second, $string);

DEMO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文