我想从数据库表中获取数组中的键和值,我需要将它添加到数组中。那么如何将其设置为数组呢?如果我使用array_push()那么它将从0开始索引,我需要从表中获取的数组索引。
我的表名为 array_val
键 值
k1 val1
k2 val2
答案 0 :(得分:0)
如果您使用的是MySQLi,我认为您正在寻找mysqli_fetch_assoc()
的内容。
答案 1 :(得分:0)
$array_val = array();
$results = mysql_fetch_array($query);
foreach($results as $key => $val){
$array_val[$key] = $val
}
答案 2 :(得分:0)
另一种解决方案
# dummy data
df1 <- read.table(text = "
From To
1 June August
2 January December
3 05/01/2013 10/30/2013
4 July November
5 06/17/2013 10/14/2013
6 05/04/2013 11/23/2013", header = TRUE, as.is = TRUE)
# custom function
myFun <- function(x, dummyDay = "01", dummyYear = "2013"){
require(lubridate)
x <- ifelse(substr(x, 1, 3) %in% month.abb,
paste(match(substr(x, 1, 3), month.abb),
dummyDay,
dummyYear, sep = "/"), x)
#return date
mdy(x)
}
res <- data.frame(lapply(df1, myFun))
res
# From To
# 1 2013-06-01 2013-08-01
# 2 2013-01-01 2013-12-01
# 3 2013-05-01 2013-10-30
# 4 2013-07-01 2013-11-01
# 5 2013-06-17 2013-10-14
# 6 2013-05-04 2013-11-23
键和值是您的表格字段。表格是: array_val
答案 3 :(得分:0)
低于代码可以帮助你
简单地说,使用while
循环并从db记录中创建数组元素。
$arr = array();
$sql = "SELECT key,value FROM <tablename>";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[$row['key']] = $row['value'];
}
}