增加一个多维数组的关键 [英] Increment the key on a multidimensional array

查看:120
本文介绍了增加一个多维数组的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PHP数组:

  $月=阵列(
    '周'=>阵列(
        'W1'=>阵列(
          D1= GT; '第一天',
          'D2'=> '第二天',
          'D3'=>阵列(
             H1=> 小时一个人,
             'H2'=> 小时两化
           )
        )
        'W2'=>阵列(
          D1= GT; '第一天',
          'D2'=> '第二天'
        )
    )
);

欲递归迭代阵列上方和更改密钥,以反映从一个月的开始的递增的时间。像这样(简体):

  $月=阵列(
    '周'=>阵列(
        '1'=>阵列(
          '1'=> '第一天',
          '2'= GT; '第二天',
          '3'= GT;阵列(
             '3'= GT; 小时一个人,
             '4'= GT; 小时两化
           )
        )
        '5'= GT;阵列(
          '5'= GT; '第一天',
          '6'=> '第二天'
        )
    )
);

请注意:因为它下调到孩子计数器不递增。这反映了开始时间为一个星期的开始时间的第一天都共享相同的时间戳。对于下一个兄弟的计数器递增和键/值对后的计数器递增一直没有孩子。


解决方案

这是'嵌套阵列与输出数组的输入结构精确匹配的例子。唯一的区别是,数组键都变成几乎递增的整数下标。

在'几乎'一部分来自当事实'筑巢'下来一个级别的第一项有标为previous水平相同。

我接近这个问题的方法是使用一个递归函数,将处理的嵌套,并且每个内一个'的foreach'循环'水平阵列中的每个条目保持在相同的中的输出电平'

输出:此需要包含很多层面,而不是存储的电流输出标中的路径参考,可以通过适当的code设定简单'的水平,我只是传递一个数组

在code: 使用PHP 5.3.19在毒蛇-7给定的输入运行

首先,做工作的功能:

注:$ destWeeks和$ idxNew变量被称为引用通过这使得code直接修改它们。

 函数reindexWeeks($ sourceWeeks,&安培; $ destWeeks,&安培; $ idxNew)
{
    的foreach($ sourceWeeks为$ sourceKey => $ sourceValue){
        如果(is_array($ sourceValue)){
            $ destWeeks [$ idxNew] =阵列();
            reindexWeeks($ sourceValue,$ destWeeks [$ idxNew],$ idxNew);
        }
        其他{
            $ destWeeks [$ idxNew] = $ sourceValue;
            $ idxNew ++;
        }
    }
}

调用code:

  $ idxNew = 1; //开始索引。
$ newWeeks =阵列(); //在'周'级输出数组reindexWeeks(每月$ ['周'],$ newWeeks,$ idxNew); //重新索引数组//输出数组..
$ newMonth =阵列('周'=> $ newWeeks);

使用问题提供的示例输出:

 阵列

    [周] =>排列
        (
            [1] =>排列
                (
                    [1] =>第一天
                    [2] =>第二天
                    [3] =>排列
                        (
                            [3] =>每小时一次
                            [4] =>小时二
                        )                )            [5] =>排列
                (
                    [5] =>第一天
                    [6] =>第二天
                )        ))

I have the following php array:

$month = array (
    'weeks'=> array(
        'w1'=> array(
          'd1'=> 'day one',
          'd2'=> 'day two',
          'd3'=> array(
             'H1' => 'hour one',
             'H2' => 'hour two' 
           )
        ),
        'w2'=> array(
          'd1'=> 'day one',
          'd2'=> 'day two'
        )
    )
);

I want to recursively iterate over the array and change the keys to reflect an incremented time from the start of the month. Like so (simplified):

$month = array (
    'weeks'=> array(
        '1'=> array(
          '1'=> 'day one',
          '2'=> 'day two',
          '3'=> array(
             '3' => 'hour one',
             '4' => 'hour two' 
           )
        ),
        '5'=> array(
          '5'=> 'day one',
          '6'=> 'day two'
        )
    )
);

Note: the counter is NOT incremented as it steps down to a child. This reflects that the start time for a Week and the start time for its First Day both share the same timestamp. The counter increments for the next sibling AND the counter increments after a 'key/value' pair has no children.

解决方案

This is an example of 'nested arrays' with the output array exactly matching the structure of the input. The only difference is that the array keys are turned into 'almost' incrementing integer subscripts.

The 'almost' part comes from the fact that when 'nesting' down one level the first entry has the same subscript as the previous level.

The way i approached this is to use a recursive function, which will deal with the 'nesting', and a 'foreach' loop within each 'level as each entry in the array remains at the same 'level' in the output.

Output: This needs to contain many levels, rather than storing a path of subscripts to the current output 'level, i just pass an array 'by reference' that can be simply set by the appropriate code.

The code: running with the given input using PHP 5.3.19 at 'viper-7'

First the function that does the work:

Note: the $destWeeks and $idxNew variables are passed as 'references' this allows the code to amend them directly.

function reindexWeeks($sourceWeeks, &$destWeeks, &$idxNew)
{
    foreach($sourceWeeks as $sourceKey => $sourceValue) {
        if (is_array($sourceValue)) {
            $destWeeks[$idxNew] = array();
            reindexWeeks($sourceValue, $destWeeks[$idxNew], $idxNew);
        }
        else {
            $destWeeks[$idxNew] = $sourceValue;
            $idxNew++;
        }
    }
}

Calling code:

$idxNew = 1;  // starting index.
$newWeeks = array(); // output array at the 'week' level

reindexWeeks($month['weeks'], $newWeeks, $idxNew); // re-index the array 

// output array..
$newMonth = array('weeks' => $newWeeks);

Output using the example provided in the question:

Array
(
    [weeks] => Array
        (
            [1] => Array
                (
                    [1] => day one
                    [2] => day two
                    [3] => Array
                        (
                            [3] => hour one
                            [4] => hour two
                        )

                )

            [5] => Array
                (
                    [5] => day one
                    [6] => day two
                )

        )

)

这篇关于增加一个多维数组的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆