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!

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);
commented: Had the perfect solution. +2

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.