-1

Possible Duplicate:
Fastest way to add prefix to array keys?

I have an array that does like this:

0 => value 1,
1 => value 2,

I want to done like this:

keyname1 => value 1,
keyname2 => value 2,

In the mySQL database, all the columns have names but the array is using numbers instead of names. Is there a PHP or mySQL command that tells it how to write the keys?

Community
  • 1
  • 1
Plummer
  • 6,522
  • 13
  • 47
  • 75
  • 2
    If the data is coming from mySQL, have you tried fetching the data _as an associative array_? `$row = $stmt->fetch(PDO::FETCH_ASSOC);`, in case you're using PDO, that fetches the first result of the query as an associative array – Elias Van Ootegem Dec 18 '12 at 20:18
  • That is exactly what I wanted. Post answer, please. – Plummer Dec 18 '12 at 20:26
  • @Plummer, posted answer, I don't know if you're using PDO or not, but I hope/assume you do. I just added the deprecated thing for anybody who might stumble on this question later. – Elias Van Ootegem Dec 18 '12 at 20:44
  • @Elias, I'm actually using Joomla, but using your answer, I was able to translate into Joomla speak and find the answer I needed. – Plummer Dec 18 '12 at 20:46

3 Answers3

1
array_walk($array, function($value, &$key){ $key = 'prefix' . $key;});
Naftali
  • 144,921
  • 39
  • 244
  • 303
1

After fetching the resultset, traverse it with a loop, and add the column names as keys to the array. here's an example,

foreach($resultset as $k => $v)
 $array[$k] = $v;

I am assuming that $resultset is an array.

Note: Mysql_* extensions are deprecated. Try to use Mysqli_* or PDO extensions instead.

Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
  • Yes. And just to be clear, instead of the array printing the index number, I want the column name. So where it would be
    NameAge
    John42
    , I want it to print `name => john, age => 42` (sorry, html table didn't come out right in comments)
    – Plummer Dec 18 '12 at 20:21
  • First of all, the table you mentioned in the comment, is HTML table and `NOT Mysql table`. Secondly, do you want an array with keys `name` and `age` or do you want to literally print something like `name => john, age => 42` ? – Teena Thomas Dec 18 '12 at 20:24
  • Show the code where you fetch the resultset. I can guide you from there. – Teena Thomas Dec 18 '12 at 20:25
  • HTML was just for example. I was hoping it would just show an table and not a bunch of markup. I want the array to look like `array ( name => john, age => 42)` from which I can pull data into something else. – Plummer Dec 18 '12 at 20:25
  • okies gotcha...then my initial answer should work for you. – Teena Thomas Dec 18 '12 at 20:26
1

In response to your comment:

You're retrieving data from a mySQL database, so you can just fetch the data as an associative array:

PDO:

//just as an example
$stmt = $pdo->prepare('SELECT foo, bar from foobar.foobaz WHERE xyz = :xyz');
$stmt->execute(array(':xyz' => $someValue));
$results = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    array_push($results, $row);
}

That should do it. Of course, there is mysql_fetch_assoc, but since the mysql_* extension is deprecated, I'm not going to bother with that, just switch to PDO, if you haven't already.

If you're not comfortable with PDO for some reason, look into mysqli_*, in which case the loop looks like this:

while($row = $stmt->fetch_assoc())
{//using OO api
    array_push($results, $row);
}
//or
while($row = mysqli_fetch_assoc($result))
{//using OO api
    array_push($results, $row);
}

full examples [on the doc pages](http://www.php.net/mysqli_fetch_assoc

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149