Php数组-丢失了一些值
我试图在数组中插入一些值,但不知道为什么会丢失其中的一些值 这是我现在正在使用的代码:Php数组-丢失了一些值,php,arrays,Php,Arrays,我试图在数组中插入一些值,但不知道为什么会丢失其中的一些值 这是我现在正在使用的代码: <pre><code> <?php include 'config/connections.php'; $SQL = "SELECT * FROM table"; $result = $db->query($SQL) or die("Couldn't execute query.".mysqli_error($db)); $numresult=$result->
<pre><code>
<?php
include 'config/connections.php';
$SQL = "SELECT * FROM table";
$result = $db->query($SQL) or die("Couldn't execute
query.".mysqli_error($db));
$numresult=$result->num_rows;
for ($i=0; $i<$numresult; $i++)
{
$row = $result->fetch_assoc();
$c['id'] = 'H'.$i;
$c['start']= date("Y-m-d");
$a[$i] = $c;
for ($j=1; $j<4; $j++)
{
$c['id'] = 'H'.$i;
$c['start']= date('Y-m-d', strtotime("+".$j." days"));
array_push($a,$c);
}
}
echo json_encode($a);
?>
</pre></code>
在第一次循环迭代中,将创建$a[0]
,然后为创建第二次循环,将3个元素添加到$a
数组中,从而创建$a[1]
到$a[4]
在第二个外部for迭代中,$a[1]
被覆盖,内部for将$a[5]
添加到$a[7]
第三个外部for迭代覆盖$a[2]
,依此类推
您可以这样做:
在$c['start']=date('Y-m-d',strotime(“+”$j.“days”);的行上出现语法错误;
应该是“Y-m-d”
。谢谢,我尝试了你的建议,但仍然没有work@Candra您还可以使用array\u push($a,$c)在第一个for循环中保留代码并更改$a[$i]=$c;
成功了,谢谢你,axxis,你救了我的命
[{"id":"H0","start":"2015-05-13"},
{"id":"H1","start":"2015-05-13"},
{"id":"H2","start":"2015-05-13"},
{"id":"H3","start":"2015-05-13"},
{"id":"H0","start":"2015-05-17"},
{"id":"H1","start":"2015-05-14"},
{"id":"H1","start":"2015-05-15"},
{"id":"H1","start":"2015-05-16"},
{"id":"H1","start":"2015-05-17"},
{"id":"H2","start":"2015-05-14"},
{"id":"H2","start":"2015-05-15"},
{"id":"H2","start":"2015-05-16"},
{"id":"H2","start":"2015-05-17"},
{"id":"H3","start":"2015-05-14"},
{"id":"H3","start":"2015-05-15"},
{"id":"H3","start":"2015-05-16"},
{"id":"H3","start":"2015-05-17"}]
{"id":"H0","start":"2015-05-14"},
{"id":"H0","start":"2015-05-15"},
{"id":"H0","start":"2015-05-16"}
<?php
include 'config/connections.php';
$SQL = "SELECT * FROM table";
$result = $db->query($SQL) or die("Couldn't execute query.".mysqli_error($db));
$numresult=$result->num_rows;
$a = array();
for ($i=0; $i<$numresult; $i++)
{
$row = $result->fetch_assoc();
for ($j=0; $j<4; $j++)
{
$c['id'] = 'H'.$i;
$c['start']= date('Y-m-d', strtotime("+".$j." days"));
array_push($a,$c);
}
}
echo json_encode($a);
?>