954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Assign SQL column values as key names in an array

Hello everyone!

I'm wondering if there is a way to assign the values of a column in SQL as the key name in a multidimensional array.

Here is what I have, my database looks like this:

Table Name = SJCFPCB

date | totalin | cdulep | icu |
-----------------------------------
2011-01-27 | 30 | 5 | 4
2011-01-11 | 28 | 4 | 5
2010-12-11 | 31 | 7 | 6

Here is the query I'm using to get the data:

$sql = "SELECT * , convert(CHAR, \"date\", 10) AS ndate from dbo.SJCFPCB where 30 > DATEDIFF(d,\"date\", GETDATE())";
$result=odbc_exec($conn1, $sql);
$DATA=array();

while ($rows=odbc_fetch_array($result)){
$DATA[]=$rows['totalin'];
}
print_r($DATA);


What returns is something like:

Array ( [0] => 30 [1] => 28 [2] => 31)

What I would like to do is rename the keys [0] [1] [2] ect.. .to the value in date field so it would look like this:

Array ( [2011-01-37] => 30 [2011-01-11] => 28 [2010-12-11] => 31)

Any ideas?

Thank you!

agr8lemon
Light Poster
30 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

You can make it by assigning the table column as array key.

while ($rows=odbc_fetch_array($result)){
$DATA[$rows['date']]=$rows['totalin'];
}
print_r($DATA);
paulrajj
Junior Poster
101 posts since Mar 2010
Reputation Points: 17
Solved Threads: 37
 

You can make it by assigning the table column as array key.

while ($rows=odbc_fetch_array($result)){
$DATA[$rows['date']]=$rows['totalin'];
}
print_r($DATA);

Perfect, Thank you!

agr8lemon
Light Poster
30 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: