title: Refactor PHP Array Grouping and Mapping
body: <p><strong>Goal</strong></p>
  
  <p>I'm wondering if there is a better way to code this problem? I'd like to refactor if possible.</p>
  
  <p><strong>Requirements:</strong></p>
  
  <ul>
  <li>Given a list of ACRISS Vehicle Codes (<a href="http://www.acriss.org/car-codes.asp" rel="nofollow noreferrer">http://www.acriss.org/car-codes.asp</a>), group the vehicles into categories based on their characters. A car may be grouped into 1 or more categories. LTAR Example: Since L is the first character it would be grouped into Luxury and since T is the second character it would also be grouped as a Convertible.</li>
  <li><strong>Compact</strong> - CB..,CC.., CD.. (any vehicle codes that begin with CB, CC, or CD. Examples: CBAR, CDAR, CCAR)</li>
  <li><strong>Economy</strong> - EB..,EC..,ED..</li>
  <li><strong>Full-Size</strong> - FB..,FC..,FD..</li>
  <li><strong>Luxury</strong> - L... (starts with L)</li>
  <li><strong>Van</strong> - .F.. (second character is F)</li>
  <li><strong>Convertible</strong> - .T.. (second character is T, example: DTAR)</li>
  <li><strong>Hybrid</strong> - ...H (fourth character is H or I, example: CCAH)</li>
  <li><strong>Electric</strong> - ...C (fourth character is C or E, example: CCAE)</li>
  </ul>
  
  <p><strong>Input Examples (each vehicle code will always have 4 characters):</strong></p>
  
  <ol>
  <li>ECAR</li>
  <li>CCAR</li>
  <li>SFAR</li>
  <li>STAR</li>
  <li>LTAR</li>
  </ol>
  
  <p>Expected Output:</p>
  
  <ol>
  <li>[category] => Array ([0] => Economy)</li>
  <li>[category] => Array ([0] => Compact)</li>
  <li>[category] => Array ([0] => SUV)</li>
  <li>[category] => Array ([0] => Convertible)</li>
  <li>[category] => Array ([0] => Luxury [1] => Convertible)</li>
  </ol>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>        $vehicle_code = str_split($new_car-&gt;type);
          $categories = [
              'CB' =&gt; 'Compact',
              'CC' =&gt; 'Compact',
              'CD' =&gt; 'Compact',
  
              'EB' =&gt; 'Economy',
              'EC' =&gt; 'Economy',
              'ED' =&gt; 'Economy',
  
              '0L' =&gt; 'Luxury',
  
              '1T' =&gt; 'Convertible',
              '1V' =&gt; 'Van',
  
              '3H' =&gt; 'Hybrid',
              '3I' =&gt; 'Hybrid',
              '3C' =&gt; 'Electric',
              '3E' =&gt; 'Electric',
          ];
  
          foreach($vehicle_code as $key=&gt;$character) {
              if (array_key_exists($key.$character,$categories))
              {
                  $new_car-&gt;category[] = $categories[$key.$character];
              }
          }
  
          if (array_key_exists(substr($new_car-&gt;type, 0, 2),$categories))
          {
              $new_car-&gt;category[] = $categories[substr($new_car-&gt;type, 0, 2)];
          }
  </code></pre>