Take the 2-minute tour ×

I know there must an easy and efficient way to do this.

I have an array like this:

       $fields = array( "EVENT_ID" => "Event Id" ,
            "EVENT_NAME"           => "Name",
            "EVENT_CLASSIFICATION" => "Classification",
            "DESCRIPTION"          => "Description",
            "START_TIME"           => "Start Time",
            "END_TIME"             => "End Time"

I would like to convert this as this query:

                   "EVENT_ID" as "Event ID",
                   "EVENT_NAME" as "Name", 

Don't want to put this in Loop. PHP has a lot array handling functions and this must be easy. I am new to php.

Any ideas?

share|improve this question
Sorry to disappoint you, but if you are afraid of loops, forget about arrays :) –  Andrejs Cainikovs Apr 17 '12 at 17:15
As well as software development in general, excepting maybe Prolog, SQL and functional-only languages. –  Seva Alekseyev Apr 17 '12 at 17:25
@AndrejsCainikovs, Forget Arrays!?! Whats better alternatives? :-) –  Kevin Rave Apr 17 '12 at 17:27
@KevinRave: use loops! –  Andrejs Cainikovs Apr 17 '12 at 17:28
But you loop arrays arrrggg :-) –  Dale Apr 17 '12 at 17:29
add comment

4 Answers

up vote 0 down vote accepted

You can simply build your query's string by looping through your values.

$query = '';
foreach($fields as $k => $v)
    $query .= '"' . $k . '" as "' . $v . '",';
echo $query;

Note that this will leave a trailing comma. If you want to remove it, you can use

$query = rtrim($query, ',');

Live example

share|improve this answer
add comment

I heavily suggest loops!

$sel = array();
  $sel[] = '`'.$key.'` AS `'.$val.'`';
$sel = implode(',', $sel);
share|improve this answer
please, correct to $sel[] = $key.' AS '.$val; –  heximal Apr 17 '12 at 17:15
Yes, thank you, I typed too fast. –  Dan Lee Apr 17 '12 at 17:16
Thanks! Not possible without loops? –  Kevin Rave Apr 17 '12 at 17:21
It is, see Rockets answer, but that's just a wrapper for a loop too. So it doesn't really matter. (although I find that harder too read than a loop) –  Dan Lee Apr 17 '12 at 17:22
Those backticks are there that you can use reserved mysql words as you want. –  Dan Lee Apr 17 '12 at 17:26
show 1 more comment

A loop is (probably) the way to go here, but you can do it without one. Using array_map.

$sql = implode(',', array_map(function($v, $k){
    return "`$k` AS `$v`";
}, $fields, array_keys($fields)));

NOTE you can only pass functions like this in PHP 5.3+. If you're using 5.2, you can use create_function.

$sql = implode(',', array_map(create_function('$v, $k', 'return "`$k` AS `$v`";'), $fields, array_keys($fields)));
share|improve this answer
Cool one liner, +1 –  Alex Turpin Apr 17 '12 at 17:23
Thanks! It looks good. Which one is efficient and fast? Loop or this one? –  Kevin Rave Apr 17 '12 at 17:24
@KevinRave functions like array_map() use loops under the bonnet anyway, there won't be much in it. That said, a C++ loop is likely more efficient than a PHP one, so this is probably slightly better. But so little difference that it's not even noticeable unless you have hundreds of thousands of items. Although this does have the added overhead of calling a function on every iteration of the loop underneath, so YMMV. –  DaveRandom Apr 17 '12 at 17:26
add comment

If you're on 5.3, you can use array_reduce with closures (plain old functions work too, but foreach would be shorter then):

$sql = array_reduce(array_keys($fields), function(&$result, $key) use ($fields) {
  if (!is_null($result)) $result .= ",\n";
  return "{$result}'{$key}' AS '{$fields[$key]}'";

Try online

share|improve this answer
I'd prefer array_map for this situation, but this works too. +1 –  Rocket Hazmat Apr 17 '12 at 17:40
add comment

Your Answer


