首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用定义的范围将键输入到数组中?

如何使用定义的范围将键输入到数组中?
EN

Stack Overflow用户
提问于 2015-06-23 12:36:49
回答 2查看 63关注 0票数 3

所以我有输出以下内容的代码:

代码语言:javascript
复制
$test = array('test1.txt' => '1 June 2015',
               'test2.txt' => '1 June 2015',
               'test3.txt' => '1 June 2015',
               'test4.txt' => '1 June 2015',
               'test5.txt' => '1 June 2015');

但不是5个文件,而是数百个文件。我如何才能定义输出的日期,这取决于行号。

如果文件1-5 (前5个文件),则日期=“2015年6月1日”。如果file 6-8 (接下来的2个文件),那么date =“2015年6月5日。等等。我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2015-06-23 13:11:12

你可以试试这个-

代码语言:javascript
复制
$date = "1 June 2015"; // The initial date
$fileNum = array(5, 2, 3); // The number of files to be considered
$total = array_sum($fileNum); // Total number of files
$array = array(); // Array to be filled
$j = 1; // The number for increment
foreach($fileNum as $num) {
    $i = 1; // The number to compare the file numbers 
    while($i <= $num) {
         $array['test' . $j . '.txt'] = $date; // Store the values
         $j++;
         $i++;
    }
    $date = date('j F Y', strtotime('+ 1 DAY', strtotime($date))); // Increment the date
}

var_dump($array);

输出

代码语言:javascript
复制
array(10) {
  ["test1.txt"]=>
  string(11) "1 June 2015"
  ["test2.txt"]=>
  string(11) "1 June 2015"
  ["test3.txt"]=>
  string(11) "1 June 2015"
  ["test4.txt"]=>
  string(11) "1 June 2015"
  ["test5.txt"]=>
  string(11) "1 June 2015"
  ["test6.txt"]=>
  string(11) "2 June 2015"
  ["test7.txt"]=>
  string(11) "2 June 2015"
  ["test8.txt"]=>
  string(11) "3 June 2015"
  ["test9.txt"]=>
  string(11) "3 June 2015"
  ["test10.txt"]=>
  string(11) "3 June 2015"
}

更新

代码语言:javascript
复制
$fileNum = array(5 => "1 June 2015", 2 => "2 June 2015", 3 => "3 June 2015"); // Set the array with file number and corresponding dates
$total = array_sum($fileNum);
$array = array();
$j = 1;
foreach($fileNum as $num => $date) {
    $i = 1;
    while($i <= $num) {
         $array['test' . $j . '.txt'] = $date;
         $j++;
         $i++;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-06-23 12:56:08

尝尝这个

代码语言:javascript
复制
  $numberOfFiles = 100;
//$startDate = '1 June 2015';

$startDate = '1 June 2015';
for($i=1;$i<=$numberOfFiles;$i++)
{
    if($i%5==0)
    {
        $test['test'.$i.'.txt'] = $startDate;
        $startDate = date('j F Y',strtotime($startDate.' + 5 days'));
    }
    else
    {
        $test['test'.$i.'.txt'] = $startDate;
    } 
}

print_r($test);

已根据要求进行更新。请注意,您必须根据文件编号定义日期。

代码语言:javascript
复制
$numberOfFiles = 50;
//$startDate = '1 June 2015';

for($i=1;$i<=$numberOfFiles;$i++)
{
    if($i<=5)
    {
        $startDate ='1 June 2015';
        $test['test'.$i.'.txt'] = $startDate;
    }
    elseif((5<$i)&&($i<=8))
    {
        $startDate ='2 June 2015';
        $test['test'.$i.'.txt'] = $startDate;
    }
    elseif((8<$i)&&($i<=50))
    {
        $startDate ='11 June 2015';
        $test['test'.$i.'.txt'] = $startDate;
    }
    else
    {
        // Define the date value if does not match above criterian
    } 
}

print_r($test);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30993953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档