我有一个问题,我无法将数组放入数组中。
结果应如下所示:
$data=array(
array(
'label' => 'Totals',
'color' => '#745fa4',
'data' =>
array(
array('01/01/2015', 125),
array('02/01/2015', 148),
array('03/01/2015', 42),
array('04/01/2015', 115),
array('05/01/2015', 45),
array('06/01/2015', 77),
array('07/01/2015', 59)
)
),
);
我目前有这个,但无法将其转换为正确的格式:
$result = mysqli_query($con,"SELECT * FROM table ORDER BY id DESC LIMIT 28");
$chartdata = array();
while($row = $result->fetch_assoc()) {
$chartdata[] .= array($row["date"]. ", " . $row["total"]);
}
$data=array(
array(
'label' => 'Totals',
'color' => '#745fa4',
'data' => $chartdata
)
);
如果有人能帮助我,那就太棒了。
1) .=
这意味着使用字符串串联进行赋值。 您应该将数组推送到父数组中
2)子数组内部有两个值而不是单个字符串,因此在两个值之间使用,
而不是字符串连接","
$chartdata[] .= array($row["date"]. ", " . $row["total"]);
自
$chartdata[] = array($row["date"], $row["total"]);
这里有一个多余的点:
# ⇓
$chartdata[] .= array($row["date"]. ", " . $row["total"]);
要将新项目推送到数组中,应使用$array[] = $item
:
$chartdata[] = array($row["date"]. ", " . $row["total"]);
点用于连接字符串。希望对您有所帮助。
删除圆点。
<?php
$result = mysqli_query($con,"SELECT * FROM table ORDER BY id DESC LIMIT 28");
$chartdata = array();
while($row = $result->fetch_assoc()) {
$chartdata[] = array($row["date"], $row["total"]); // Remove the dot
}
$data=array(
array(
'label' => 'Totals',
'color' => '#745fa4',
'data' => $chartdata
)
);