3

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?

8
  • How are you defining your range. or on what basis you have your range Commented Jun 23, 2015 at 4:47
  • The number of files are fixed or they may vary? Or how the number will be defined? Commented Jun 23, 2015 at 4:54
  • $date = $test['test'.$n.'.txt'] where $n = filenumber Commented Jun 23, 2015 at 4:54
  • The number of files may vary. The dates are defined manually by me for range (i.e. i define 1 date for first 5 files, define another for next 2, etc. etc.) Commented Jun 23, 2015 at 4:56
  • Then how one can decide the number of files? There must be some way that tell about that!! Commented Jun 23, 2015 at 4:57

2 Answers 2

1

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++;
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Hey, thanks for the reply. This is my actual code, can it integrate with this? $path = "/home/files/"; $date = "1 June 2015"; $files = array();foreach (glob($path . "*.txt") as $filename) { $files[$filename] = ""; var_export($files);
What does array(5, 2, 3) do? How would I go about doing that? Sorry, I am quite a beginner when it comes to PHP.
It is the number of files.
I don't want to increment dates by 1 I want to define using something like this: $files1to5 = "1 June 2015"; $files6to8 = "2 June 2015"; $files9to50 = "11 June 2015";
I ran the code but it had no output. How would I add my "$files" array in the code $path = "/home/files/"; $date = "1 June 2015"; $files = array();foreach (glob($path . "*.txt") as $filename) { $files[$filename] = ""; var_export($files);
|
0

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

3 Comments

Are you sure the number of files will be 5?
Need a specific pattern to increment date against file number.
Thanks! I'm testing it out right now, will comment back results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.