如何将值从一个数组赋值给另一个数组n次

时间:2014-06-24 03:36:21

标签: php arrays datetime

我正在尝试创建一个使用特定项目的日程表。

我有2个数组。 日期& 另一个名字以及他们可以使用该项目的天数。

我已设法使用此功能创建日期数组。

function dateArray($from, $to, $value = NULL) {
    $begin = new DateTime($from);
    $end = new DateTime($to);
    $interval = DateInterval::createFromDateString('1 day');
    $days = new DatePeriod($begin, $interval, $end);

    $baseArray = array();
    foreach ($days as $day) {
        $dateKey = $day->format("d-m-Y");
        $baseArray[$dateKey] = $value;
    }

    return $baseArray;
}

$dates_array = dateArray('01-01-2014', '30-09-2014',true);

print_r($dates_array );

给我的日期为

Array
(
    [01-01-2014] => 1
    [02-01-2014] => 1
    [03-01-2014] => 1
    [04-01-2014] => 1
    [05-01-2014] => 1
    [06-01-2014] => 1
    [07-01-2014] => 1
    [08-01-2014] => 1
    [09-01-2014] => 1
and so on.
)

我有另一组名称名称为关键天数为,他们可以使用此类项目。

$names_array = array("name1" => "4", "name2" => "3", "name3" => "1");

现在我想根据个人可以使用该项目的天数为日期指定名称。 像这样。 我需要我的最终输出数组是这样的

Array
(
    [01-01-2014] => name1
    [02-01-2014] => name1
    [03-01-2014] => name1
    [04-01-2014] => name1
    [05-01-2014] => name2
    [06-01-2014] => name2
    [07-01-2014] => name2
    [08-01-2014] => name3
    [09-01-2014] => name1
and so on. notice name1 comes again 
)

所以我试图获得如上所述的输出,但我在foreach中的while循环失败了。 到目前为止,我已经尝试过了。

function dateArray($from, $to, $value = NULL) {
    $begin = new DateTime($from);
    $end = new DateTime($to);
    $interval = DateInterval::createFromDateString('1 day');
    $days = new DatePeriod($begin, $interval, $end);

    $baseArray = array();
    foreach ($days as $day) {
        $dateKey = $day->format("d-m-Y");
        $baseArray[$dateKey] = $value;
    }

    return $baseArray;
}

$dates_array = dateArray('01-01-2014', '30-09-2014',true);

$names_array = array("name1" => "4", "name2" => "3", "name3" => "1");

print_r($dates_array );

$new_dates = array();

foreach($dates_array as $dates => $key){
//echo $dates;
    foreach ($names_array as $name => $days){

        while($days <= 1){
        $new_dates[$dates] = $name ;
        $days = $days - 1;
        }
    }
}

print_r($new_dates);

但是我的最后一个数组是空的。

所以我该如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

您可以使用MultipleIterator,第二个数组(名称)在需要时循环播放:

$names_array = array();

// unwind the array values
foreach (array("name1" => "4", "name2" => "3", "name3" => "1") as $value => $freq) {
    for ($i = 0; $i < $freq; ++$i) {
        $names_array[] = $value;
    }
}

// attach both arrays
$m = new MultipleIterator;
$m->attachIterator(new ArrayIterator(array_keys($dates_array)));
$m->attachIterator(new InfiniteIterator(new ArrayIterator($names_array)));

// build final array
$result = array();
foreach ($m as $value) {
    $result[$value[0]] = $value[1];
}

答案 1 :(得分:0)

应该是这样的......

while($days <= 1){
    $new_dates[$dates] = $name ;
    $names_array[$name] = $days - 1;
}

答案 2 :(得分:0)

您好,您可以试试这个

$dates_array = array_keys(dateArray('01-01-2014', '30-09-2014',true));
$names_array = array("name1" => "4", "name2" => "3", "name3" => "1");
$new_dates = array();

$daysindex = 0;
while($daysindex < count($dates_array)){
    foreach ($names_array as $name => $day){
        for($x = 0; $x<$day; $x++){
            if(isset($dates_array[$daysindex])){
            $new_dates[$dates_array[$daysindex]] = $name ;
        $daysindex++;
            }
        }
    }
}
print_r($new_dates);

答案 3 :(得分:0)

$dates = dateArray('01-01-2014', '30-09-2014',true);  
$names = array('name1' => 4,'name2' => 3,'name3' => 1);

$count = current($names);
$name = key($names);

foreach($dates as &$value) {   
    $value = $name;

    //if we've displayed a name $count times, move to the next name
    if(--$count == 0) {
        //if we're at the end of $names, wrap around
        if(false === next($names)) {
            reset($names);
        }

        $count = current($names);
        $name = key($names);        
    }
}

print_r($dates);