PHP:使用单独数组中定义的路径设置多维关联数组元素的值

发布于 2024-11-09 11:40:04 字数 1240 浏览 0 评论 0原文

好的,我有一个包含以下元素的数组:

$array['a']['b'][0]['c'];
$array['a']['b'][1]['c'];
$array['a']['d'][0]['c']['c'];
$array['b']['c'];

然后在一个单独的数组中,我定义了这些值的路径:

$structure[0] = array('a','b','#','c');
$structure[1] = array('a','d','#','c','c');
$structure[2] = array('b','c');

最后,我有一个包含这些值的数组:

$values[0] = array('value0-0','value0-1');
$values[1] = array('value1-0');
$values[2] = array('value2-0');

我试图找到一个简单的函数/循环,它将能够将 $values 中的值应用到 $struct 中定义的 $array 的数组路径。

最终结果将是:

$array['a']['b'][0]['c']='value0-0';
$array['a']['b'][1]['c']='value0-1';
$array['a']['d'][0]['c']['c']='value1-0';
$array['b']['c']='value2-0';

在 $values[0] 或 $values[1] 的情况下,它将能够循环遍历每个值,并将与“#”匹配的 $s​​truct 元素替换为该特定 $value 的迭代编号。

这只是一个简单的情况,就是认真编写一个冗长的递归函数,还是有一个智能构造或 php 函数可以提供更优雅的解决方案?

解决方案:

感谢 Mario,我最终的解决方案是:

foreach ($struct as $i=>$keys)
  foreach ($values[$i] as $val) {
    $r = & $array;

    foreach ($keys as $key) {

        if ($key == "#") { $key = $i; }

        $r = & $r[$key];    // move pointer to subarray
    }

    $r = $val;
  }
}

Ok so I have an array that holds the following elements:

$array['a']['b'][0]['c'];
$array['a']['b'][1]['c'];
$array['a']['d'][0]['c']['c'];
$array['b']['c'];

Then in a separate array, I have defined the path to these values:

$structure[0] = array('a','b','#','c');
$structure[1] = array('a','d','#','c','c');
$structure[2] = array('b','c');

Finally, I have an array holding the values:

$values[0] = array('value0-0','value0-1');
$values[1] = array('value1-0');
$values[2] = array('value2-0');

I'm trying to find a simple function/loop that will be able to apply the values in $values to the array path of $array that is defined in $structure.

The end result would be:

$array['a']['b'][0]['c']='value0-0';
$array['a']['b'][1]['c']='value0-1';
$array['a']['d'][0]['c']['c']='value1-0';
$array['b']['c']='value2-0';

In the case of $values[0] or $values[1], it would be able to loop through each value and substitute the $structure element matching '#' with the iteration number for that particular $value.

Is this simply a case of knuckling down and writing a drawn out recursive function, or is there a smart construct or php function that could provide a more elegant solution?

SOLUTION:

Thanks to Mario, my eventual solution is:

foreach ($struct as $i=>$keys)
  foreach ($values[$i] as $val) {
    $r = & $array;

    foreach ($keys as $key) {

        if ($key == "#") { $key = $i; }

        $r = & $r[$key];    // move pointer to subarray
    }

    $r = $val;
  }
}

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

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

发布评论

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

评论(1

相思故 2024-11-16 11:40:04

您必须使用引用来遍历目标数组:

function inject($array, $struct, $values) {

    foreach ($struct as $i=>$keys)
    foreach ($values[$i] as $val) {
        $r = & $array;

        foreach ($keys as $key) {

            if ($key == "#") { $key = count($r); }

            settype($r[$key], "array");
            $r = & $r[$key];    // move pointer to subarray
        }

        $r = $val;
    }

You will have to work with references to traverse the target array:

function inject($array, $struct, $values) {

    foreach ($struct as $i=>$keys)
    foreach ($values[$i] as $val) {
        $r = & $array;

        foreach ($keys as $key) {

            if ($key == "#") { $key = count($r); }

            settype($r[$key], "array");
            $r = & $r[$key];    // move pointer to subarray
        }

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