开发者

What is the best way to add a single quote mark for an existing array key?

开发者 https://www.devze.com 2023-02-15 17:38 出处:网络
This is the code I used to get array key with single quote mark: // the array with key and value $savedFilesIds = array(\"F564574\"=>\"none\",\"F456735\"=>\"none\",\"F4777\"=>\"none\")

This is the code I used to get array key with single quote mark:

// the array with key and value
$savedFilesIds = array("F564574"=>"none","F456735"=>"none","F4777"=>"none")   

//$file_ids = imp开发者_如何学运维lode(',',array_keys($savedFilesIds));  // without adding single quote mark for keys

// the way I used to adding single quote mark for keys

$file_ids = array();

foreach($savedFilesIds as $key=>$value){
    $item = '\''.$key.'\'';  // adding single quote mark here
    array_push($file_ids , $item);  // and then adding to array  
}

$file_ids = implode(',',$file_ids);  // get the key with single quote mark

echo $file_ids;

Is there other better way to make it more effective?


$quotedIds = array_map(create_function('$a', 'return "\'$a\'";'), array_keys($savedFilesIds));


foreach ($array as $k => $v)
{
    $array["'$k'"] = $v; 
    unset($array[$k]); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消