views:

159

answers:

1

hi there,

currently i am doing this:

$values = array(
  'id'                    =>  $sel['id'],
  'creator_id'            =>  $sel['creator_id'],
  'campaign_id'           =>  $sel['campaign_id'],
  'save_results'          =>  $sel['save_results'],
  'send_results_url'      =>  $sel['send_results_url'],
  'reply_txt'             =>  $sel['reply_txt'],
  'allow_multiple_votes'  =>  $sel['allow_multiple_votes']
    );
    $cols = '';
    $vals = '';
    $first = true;
    foreach($values as $col => $val) {
        if(!$first) {
            $cols .= ', ';
            $vals .= ', ';
        }
        $cols .= $col;
        $vals .= $val;
        $first = false;
    }

the part that bothers me is this:

foreach($values as $col => $val) {
  if(!$first) {
    $cols .= ', ';
    $vals .= ', ';
  }
  $cols .= $col;
  $vals .= $val;
  $first = false;
}

is there a way to implode keys?

for example i could do

$vals = implode(', ', $values);

too implode the values but i need to do this as well for the keys.


update:
i could use

$keys = array();
    foreach($values as $col => $val)
        $keys[] = $col;
    $cols = implode(', ', $keys);
    $rows = implode(', ', $values);

but it still requires me to loop over it creating another array, surely there is a better way, do just get the keys?

+5  A: 
$cols = implode(', ',array_keys($values));
Austin Hyde
awesome, thanks :)
Hailwood