为什么PHP中的2d数组处理不是';不好?
我试图用PHP构建一个2d数组,使用两个不同的查询生成两个循环。 由于某些原因,该页面未显示任何有关所发生错误的消息。 它的反应就像数组不存在一样 因此我假设它是因为我没有正确处理数组 代码如下:为什么PHP中的2d数组处理不是';不好?,php,arrays,Php,Arrays,我试图用PHP构建一个2d数组,使用两个不同的查询生成两个循环。 由于某些原因,该页面未显示任何有关所发生错误的消息。 它的反应就像数组不存在一样 因此我假设它是因为我没有正确处理数组 代码如下: $stuarr= array(); ***//the 2d array initialization*** $query1 = "SELECT * FROM students WHERE userid= '$uid' ORDER BY name"; $stulist = mysqli
$stuarr= array(); ***//the 2d array initialization***
$query1 = "SELECT * FROM students WHERE userid= '$uid' ORDER BY name";
$stulist = mysqli_query($conn,$query1) or die ("cannot query the table1 " .mysqli_error($conn));
while ($students = mysqli_fetch_array($stulist)) {
${$students['name']}= array(); ******//the inside array initialization...the one that will insert to $stuarr***
$count=$_SESSION['counter'];
$sname=$students['name'];
$query3 = "SELECT * FROM ranks WHERE userid= '$uid' AND rankers='$sname' ORDER BY therate DESC";
$stur = mysqli_query($conn,$query3) or die ("cannot query the table1 " .mysqli_error($conn));
while ($sturank = mysqli_fetch_array($stur) && !$count==0) {
array_push(${$students['name']},$sturank['rated']);
$count=$count-1;
print_r(${$students['name']});
}
array_push($stuarr,${$stulist['name']});
print_r($stuarr); ***///this print is showing nothing***
}
我很想听听你对代码的看法
谢谢大家! 变化
array_push($stuarr,${$stulist['name']});
进入
为什么要使用这样的变量?只需执行
$student=array()
,将数据放入其中,并以$stuarr[]=$student完成
…我不确定您是否理解${$students['name']}
实际上在做什么。@PatrickQ那么您解释得太好了。$students['name']是查询中的一个字段,我想为每个名称启动一个新的变量…为什么这是一个解决方案,请解释一下。谢谢您,它正在工作,但并不像我预期的那样。。。它打印:数组([0]=>数组([0]=>[1]=>[2]=>)[1]=>数组([0]=>[1]=>[2]=>)[2]=>数组([0]=>[1]=>[2]=>))它插入o,1,2个值,而不是真实值
array_push($stuarr,${$students['name']});