title: Prefixing array keys with a string (:) in PHP
body: <p>Quick one; I know a solution, but I'm looking for something more elegant if it exists.</p>
  
  <p>I'm using PDO for prepeared statements:</p>
  
  <pre><code>$sql = "INSERT INTO my_table (foo, bar, baz) VALUES (:foo, :bar, :baz)";
  
  $source-&gt;execute($sql, array(
      ':foo' =&gt; $foo,
      ':bar' =&gt; $bar,
      ':baz' =&gt; $baz,
  ));
  </code></pre>
  
  <p>This is fine, but I want to pass in a previously created array, however the keys contained aren't prefixed by the colon (<code>:</code>), and I figure there must be an elegant way to take:</p>
  
  <pre><code>$array = array(
      'foo' =&gt; 'some',
      'bar' =&gt; 'random',
      'baz' =&gt; 'value',
  );
  </code></pre>
  
  <p>And translate it into:</p>
  
  <pre><code>$array = array(
      ':foo' =&gt; 'some',
      ':bar' =&gt; 'random',
      ':baz' =&gt; 'value',
  );
  </code></pre>
  
  <p>Without doing:</p>
  
  <pre><code>$temp = array();
  foreach($array as $key =&gt; $value){
      $temp[':' . $key] = $value;
  }
  $array = $temp;
  </code></pre>
  
  <p>I've browsed the PHP docs, but I can't find a function (<em>or sequence of</em>) that suits the purpose.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <h2>Addendum</h2>
  
  <p>Leaving the accepted answer, but +1 @chim for his clever 1-liner; solves the X in my XY problem. Reformatted solution:</p>
  
  <pre><code>$format = ':%s';
  $values = array_flip(array_map(function ($key) use($format) {
      return sprintf($format, $key);
  }, array_flip($values)));
  </code></pre>
  
  <p>Wrapped in a function, perhaps <code>array_keys_format(array $array, $format)</code></p>