I am trying to blow up an array of both my keys and values. I can easily get the keys using implode, but find that I have to repeat myself for the keys.
I am currently 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 any way to explode array keys?
For example, I could do
$vals = implode(', ', $values);
to unleash the values, but I need to do this also for keys.
I could also use
$keys = array();
foreach($values as $col => $val)
$keys[] = $col;
$cols = implode(', ', $keys);
$rows = implode(', ', $values);
but I still need to get stuck in it, creating another array, of course, is there a better way, just get the keys?