Fast; I know the solution, but I'm looking for something more elegant if it exists.
I am using PDO for new operators:
$sql = "INSERT INTO my_table (foo, bar, baz) VALUES (:foo, :bar, :baz)"; $source->execute($sql, array( ':foo' => $foo, ':bar' => $bar, ':baz' => $baz, ));
This is fine, but I want to pass the previously created array, however, the keys contained in it are not a colon prefix ( :
, and I believe that there should be an elegant way:
$array = array( 'foo' => 'some', 'bar' => 'random', 'baz' => 'value', );
And translate it into:
$array = array( ':foo' => 'some', ':bar' => 'random', ':baz' => 'value', );
Without execution:
$temp = array(); foreach($array as $key => $value){ $temp[':' . $key] = $value; } $array = $temp;
I looked through the PHP docs, but I cannot find a function (or sequence) that is suitable for this purpose.
Any ideas?
Adding
Leaving the accepted answer, but +1 @chim for his smart 1-liner; solves X in my XY problem. Reformed decision:
$format = ':%s'; $values = array_flip(array_map(function ($key) use($format) { return sprintf($format, $key); }, array_flip($values)));
Function completed, possibly array_keys_format(array $array, $format)