所以我的代码输出如下:
$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个文件而不是5个文件。如何制作,以便我可以根据行号定义输出的日期,即
如果文件1-5(前5个文件),则日期=" 2015年6月1日"。如果文件6-8(接下来的2个文件),则日期=" 2015年6月5日。等等。我将如何这样做?
答案 0 :(得分:1)
你可以试试这个 -
$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);
<强>输出强>
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"
}
<强>更新强>
$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 :(得分:0)
试试这个
$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);
已更新至要求。 注意您必须根据文件编号定义日期。
$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);