Quick one; I know a solution, but I\'m looking for something more elegant if it exists.
I\'m using PDO for prepeared statements:
$sql = \"INSERT INTO
$source->execute($sql, array(
'foo' => $foo,
'bar' => $bar,
'baz' => $baz
));
This is presuming the above calls PDOStatement::execute()
under the hood, with the above array
as its argument.1
:)
5.2.17
and 5.3.8
here, and working as expected.
One liner...
$array = array('test'=>'55','yest'=>'66');
$array = array_flip(array_map(function($v){return ':' . $v;},array_flip($array)));
// array(':test'=>'55',':yest'=>'66');
However this isn't safe as array_flip relies on all values being unique.
So one of the looping solutions is probably the best, or alternatively array_keys with array_combine.
use the php map-function: http://php.net/manual/en/function.array-map.php
function reduce($key)
{
if(strpos($key,":")===0)
return substr($key,1);
return $key;
}
$array = array_map("reduce",$array);
It has already been answered but this is what I came up with anyway.
$arr = array('foo'=>1,'bar'=>2);
$arr = array_flip($arr);
array_walk($arr,create_function('&$v,$k', '$v = ":$v";'));
$arr = array_flip($arr);
print_r($arr);