Prefixing array keys with a string (:) in PHP

后端 未结 4 600
臣服心动
臣服心动 2021-01-18 10:56

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         


        
4条回答
  •  死守一世寂寞
    2021-01-18 11:33

    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.

提交回复
热议问题