在数据库表中我有Value列。有超过50行。现在在php中,我有一个10的固定数组。最初将从数据库中提取10行并存储到数组中。当数组将满时,第11个值将尝试插入数组,第0个值将移动,第11个值将插入数组。例如像QUEUE过程。
首先会显示
[[0,17.9999],[1,13.898],...[9,16.98]]
然后[0,17.9999] will move and [10,11.88]
将插入,它将打印如下
[[1,13.898],.......[9,16.98],[10,11.88]]
然后
[2,17.84],......[10,11.88],[11,999]]
依旧......
我如何在php中表示它?
这是我的代码:
<?php
include("md.php");
$sql = "SELECT * from datatable";
$result =oci_parse($conn, $sql);
$r=oci_execute($result);
$arr = array();
$i=0;
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = array_shift(array($i++, (float)$row['VALUE']));
}
echo json_encode($arr);
?>
请帮忙。
答案 0 :(得分:0)
如果阵列中总是有10个键,请尝试
$arr=array_slice($arr,1,9);
$arr[]=$row['VALUE'];
而不是
$arr[] = array_shift(array($i++, (float)$row['VALUE']));
注意,当你已经有10个项目的数组时,它是正确的。 见下面的例子
$arr = array(1,2,3,4,5,6,7,8,9,10);
echo json_encode($arr)."<br>";
$con = mysqli_connect('127.0.0.1','app_user2','qwe123','test');
$select="select id from adv where id > 300 and id < 312;";
$res = mysqli_query($con, $select);
while ($row=$res->fetch_assoc()) {
$arr=array_slice($arr,1,9);
$arr[]=$row['id'];
echo json_encode($arr)."<br>";
}
在浏览器中,它表示为
[1,2,3,4,5,6,7,8,9,10]
[2,3,4,5,6,7,8,9,10,"301"]
[3,4,5,6,7,8, 9,10,"301","302"]
[4,5,6,7,8,9,10,"301","302","303"]
[5,6,7,8,9,10,"301","302","303","304"]
[6,7,8,9,10,"301","302","303","304","305"]
[7,8,9,10,"301","302","303","304","305","306"]
[8,9,10,"301","302","303","304","305","306","307"]
[9,10,"301","302","303","304","305","306","307","308"]
[10,"301","302","303","304","305","306","307","308","309"]
["301","302","303","304","305","306","307","308","309","310"]
["302","303","304","305","306","307","308","309","310","311"]
答案 1 :(得分:0)
如果我理解,在50个参赛作品中你只会得到10个,而第11个正在推开阵列的第一个值...这就像得到最后10个结果一样,你不觉得吗?
承认您的表格有一个自动递增的“id”字段:
<?php
include("md.php");
// I've changed your SQL query by adding a reverse sort
// on the 'id' field and put a limit of 10 entries
$sql = "SELECT * from datatable ORDER BY id DESC LIMIT 0,10";
$result =oci_parse($conn, $sql);
$r=oci_execute($result);
$arr = array();
$i=0;
while($row = oci_fetch_array($stid, OCI_ASSOC)){
$arr[] = (float)$row['VALUE'];
}
echo json_encode($arr);
?>
我没有测试过这个片段,但它应该可以正常工作;)
编辑:如果Ilya的回答是你想做的话,请忽略D: