How to enter keys into array using defined ranges?

68 views Asked by At

So I have code that outputs the following:

$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');

But instead of 5 files there are hundreds. How would I make it so the I can define the date outputted depending on the line number i.e.

If file 1-5 (first 5 files), then date = "1 June 2015". If file 6-8 (next 2 files), then date = "5 June 2015. Etc. Etc. How would I go about doing so?

2

There are 2 answers

3
Dinuka Dayarathna On

Try This

  $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);

Updated to requirement. Note You have to define the date against file number.

$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);
10
Sougata Bose On

You can try this -

$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);

Output

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"
}

Update

$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++;
    }
}