title: PHP to form string like "A, B, C, and D" from array values
body: <p>Given the following array:</p>
  
  <pre><code>Array
  (
      [143] =&gt; Car #1
      [144] =&gt; Car #2
      [145] =&gt; Car #3
  )
  </code></pre>
  
  <p>I am currently using this </p>
  
  <pre><code>implode(', ', array_values($car_names))
  </code></pre>
  
  <p>to generate a string like</p>
  
  <blockquote>
    <p>Car #1, Car #2, Car #3</p>
  </blockquote>
  
  <p>I would like to actually get something like </p>
  
  <blockquote>
    <p>Car #1, Car #2 and Car #3</p>
  </blockquote>
  
  <p>The idea would be to insert <strong>" and "</strong> between the two last elements of the array.</p>
  
  <p>If the array happens to contain two key/value pairs (eg, user has 2 cars), there would be no commas.</p>
  
  <blockquote>
    <p>Car #1 and Car #2</p>
  </blockquote>
  
  <p>And if the array contains one key/value (eg, user has 1 car)</p>
  
  <blockquote>
    <p>Car #1</p>
  </blockquote>
  
  <p>Any suggestion how to get this done? I tried using <code>array_splice</code> but I'm not sure that's the way to go (ie, inserting a new element into the array).</p>
  
  <p>Thanks for helping!</p>