使用用于迭代的特殊循环向Array中添加元素


Adding elements to an Array using a special loop for iteration

假设我有一个包含x元素的Array。我想以"十"为单位循环Array,并添加一个run-Time变量。这意味着每组10个Arrays将有一个唯一的run-time。我使用下面的代码:

$time = '00:00:00';
 $k = 0;
    for ($i = 0; $i < count($agnt_arr); $i+=10) {
        $temp = strtotime("+$k minutes", strtotime($time));
        $runTime = date('H:i', $temp);
        array_push($agnt_arr[$i], $runTime);
        $k+=4;
    }

其中$agnt_arrArray,结构如下:

Array
(
    [0] => Array
        (
            [name] => User.One 
            [email] => User.One@mail.com
        )
    [1] => Array
        (
            [name] => User.Two 
            [email] => User.Two@mail.com
        )
    [2] => Array
        (
            [name] => User.Three 
            [email] => User.Three@mail.com
        )
)

我遇到的问题是运行时间只添加到预期的第10个元素,但我希望元素0-9具有相同的运行时间和10-20等。我该如何实现这样的目标??

这样可能更容易,总是添加运行时,但每10更新一次:

$time = '00:00:00';
foreach($agent_arr as $key => $value) {
    if($key % 10 === 0) {
        $temp = strtotime("+$k minutes", strtotime($time));
        $runTime = date('H:i', $temp);
    }
    $agent_arr[$key]['runtime'] = $runTime;
}

这是我过于复杂的解决方案(可能是不必要的):

$new_array = array();
foreach(array_chunk($array, 10 /* Your leap number would go here */) as $k => $v)
{
    array_walk($v, function($value, $key) use (&$new_array){
        $value['time'] = $time;
        $new_array[] = $value;
    });
}