title: PHP array foreach and display all values that match a pattern
body: <p>I'm using scandir to get a list of all of the access logs in the logs directory and can display the different months of access logs if the <code>$access_log</code> = certain criteria which is based on domain and month.</p>
  
  <p>What I want to do is simplify this code so I don't have to write out all of these if statements and month variables.</p>
  
  <p>For instance if there are 100 access logs for each domain I'd have to write out all these if statements 100 times.</p>
  
  <p>An access log looks like this <code>subdomain.mydomain.com-Jan-2016.gz</code>.</p>
  
  <p>So I'm trying to figure out how to rewrite my code to have only 1 if statement that looks for a pattern of a <code>$domain</code> in the <code>$access_log</code> and lists those access logs in the <code>$log_month</code> variable. So far can't figure it out unless I write a separate if statement for each <code>$log_month</code> variable as you can see below in the 3 if statement examples within the foreach loop.</p>
  
  <pre><code>$access_logs = scandir($log_list);
  
  $month = date("M-Y");
  $prevmonth = date('M-Y', strtotime("first day of last month"));
  $minus_2_month = date('M-Y', strtotime("first day of -2 month"));
  $minus_3_month = date('M-Y', strtotime("first day of -3 month"));
  $minus_4_month = date('M-Y', strtotime("first day of -4 month"));
  //etc etc etc
  
      foreach($access_logs as $access_log) {
  
              if ($access_log == $domain_dashes . '.mydomain.com-' . $month . '.gz') {
                  $log_month = '&lt;li&gt;&lt;a href="logs.php?log=' . $access_log . '"&gt;' . $access_log . '&lt;/a&gt;&lt;/li&gt;' . "\n            ";
              }
  
              if ($access_log == $domain_dashes . '.mydomain.com-' . $prevmonth . '.gz') {
                  $log_month .= '&lt;li&gt;&lt;a href="logs.php?log=' . $access_log . '"&gt;' . $access_log . '&lt;/a&gt;&lt;/li&gt;' . "\n            ";
              }
  
              if ($access_log == $domain_dashes . '.mydomain.com-' . $minus_2_month . '.gz') {
                  $log_month .= '&lt;li&gt;&lt;a href="logs.php?log=' . $access_log . '"&gt;' . $access_log . '&lt;/a&gt;&lt;/li&gt;' . "\n            ";
              }
  
             // etc etc etc
      }
  </code></pre>
  
  <p><strong>Update Jan 19 2016</strong></p>
  
  <p>To be more specific, this is what I need. A list of domains with the access logs next to each domain.</p>
  
  <p><strong>Domain 1 - domain1.com-jan-2016.gz | domain1.com-dec-2015.gz</strong></p>
  
  <p><strong>Domain 2 - domain2.com-jan-2016.gz</strong></p>
  
  <p><strong>Domain 3 - domain3.com-jan-2016.gz | domain3.com-dec-2015.gz | etc | etc</strong></p>
  
  <p>etc</p>
  
  <p>etc</p>
  
  <p>next to each domain I need it to display the access log files. For domain 1 there might be 2 or 3 files from various months. For domain 2 there might be only 1 file. For domain 3 there might be a dozen.</p>
  
  <p>The way I have my code above works for what I need. But I wanted to condense the code with || or one simple if statement, but when I do that, then not all the access logs get displayed next to each domain. Only one file will get displayed.</p>
  
  <p>How do I get all the access log files next to each domain name with a condensed version of my code above?</p>