gpt4 book ai didi

javascript - PHP 数组转换为 JavaScript 数组

转载 作者:行者123 更新时间:2023-11-28 16:57:21 25 4
gpt4 key购买 nike

我正在尝试将 PHP 数组分配给 Javascript 数组,这样我就可以将它用于我的 Chart.js 文件中。我尝试将其添加到 javascript 数组中,但它不起作用,这是我的:这是我的 PHP 收集表单,用于将数据存储到数组中:

$id = $_SESSION['userId']; 
$dBname = "infosensor";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);

$sql = "SELECT dias FROM `$id`;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
// H:i:s -> Hora/minuto/segundo (H maior tem diferenca na forma de mostrar o horario, H maior -> 0h a 24h h menor -> 0h 12h)
// d/m/Y -> dia/mes/ano
$horario = (date('H:i:s', strtotime(str_replace('.', '-', $row['dias']))));

$tempo[] = explode(',', $horario);

}
}

打印数组 $tempo 得到的结果是:

Valor: Array ( [0] => Array ( [0] => 16:29:47 ) [1] => Array ( [0] => 16:30:07 ) [2] => Array ( [0] => 16:33:55 ) [3] => Array ( [0] => 16:34:25 ) [4] => Array ( [0] => 16:34:41 ) [5] => Array ( [0] => 16:59:26 ) [6] => Array ( [0] => 16:59:36 ) [7] => Array ( [0] => 17:02:38 ) [8] => Array ( [0] => 17:05:55 ) [9] => Array ( [0] => 17:08:46 ) [10] => Array ( [0] => 17:08:47 ) [11] => Array ( [0] => 17:09:10 ) [12] => Array ( [0] => 17:45:25 ) [13] => Array ( [0] => 18:01:06 ) [14] => Array ( [0] => 18:05:55 ) [15] => Array ( [0] => 18:20:56 ) [16] => Array ( [0] => 19:26:19 ) [17] => Array ( [0] => 20:33:12 ) [18] => Array ( [0] => 20:33:17 ) [19] => Array ( [0] => 00:00:00 ) [20] => Array ( [0] => 09:17:48 ) [21] => Array ( [0] => 15:32:21 ) [22] => Array ( [0] => 15:40:18 ) [23] => Array ( [0] => 15:41:32 ) [24] => Array ( [0] => 23:51:15 ) [25] => Array ( [0] => 23:51:22 ) [26] => Array ( [0] => 23:56:30 ) [27] => Array ( [0] => 21:51:57 ) [28] => Array ( [0] => 21:52:18 ) [29] => Array ( [0] => 21:52:31 ) )

我需要像这样存储这些数据:Obs:这只是我需要如何存储它的一个示例。

var date = ["20:01:10", "10:23:20", "23:50:20", "07:23:20"];

最佳答案

您需要使用 JSON 将数组传递给 javascript。

var date = <?php echo json_encode($yourArray) ?>

关于javascript - PHP 数组转换为 JavaScript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58754381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com