Php 如何使用定义的范围将键输入数组?
因此,我有输出以下内容的代码:Php 如何使用定义的范围将键输入数组?,php,arrays,associative-array,php-5.3,date-range,Php,Arrays,Associative Array,Php 5.3,Date Range,因此,我有输出以下内容的代码: $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 20
$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日”。如果是文件6-8(接下来的两个文件),那么date=“2015年6月5日。等等。我将如何执行此操作?尝试此操作
$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=100;
//$startDate='2015年6月1日';
$startDate='2015年6月1日';
对于($i=1;$i你可以试试这个-
$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);
更新
$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++;
}
}
$fileNum=array(5=>“2015年6月1日”,2=>“2015年6月2日”,3=>“2015年6月3日”);//使用文件号和相应日期设置数组
$total=数组和($fileNum);
$array=array();
$j=1;
foreach($fileNum作为$num=>$date){
$i=1;
while($i)您是如何定义您的范围的。或者您的范围是基于什么基础的?文件的数量是固定的,或者它们可能会变化?或者如何定义数字?$date=$test['test'.$n..txt']
其中$n=filenumber
文件的数量可能会变化。日期由我为范围手动定义(即,我为前5个文件定义1个日期,为下2个文件定义另一个日期,以此类推)那么如何确定文件的数量呢?一定有某种方法可以说明这一点!!你确定文件的数量将是5
?需要一个特定的模式来根据文件编号增加日期。谢谢!我现在正在测试,将对结果进行注释嘿,谢谢你的回复。这是我的实际代码,它能与此集成吗?$path=“/home/files/”;$date=“2015年6月1日”;$files=array();foreach(glob($path.*.txt)作为$filename){$files[$filename]=”;var_导出($files);数组(5、2、3)是什么是吗?我该怎么做呢?抱歉,我对PHP是一个初学者。它是文件的数量。我不想将日期增加1,我想用这样的方式定义:$files1to5=“2015年6月1日”$files6to8=“2015年6月2日”$files9to50=“2015年6月11日”;我运行了代码,但没有输出。我该如何添加我的“$files”“代码中的数组$path=“/home/files/”;$date=“2015年6月1日”;$files=array();foreach(glob($path.*.txt)作为$filename){$files[$filename]=”;var_export($files);
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++;
}
}