下面2个for是我的想法,写的不对,应该怎么写,要140个a1 a2 a3
不循环就这样
public $a1;
$user->a1 = $row['a1'];
//这部分的功能是读表数据并且转为json格式,便于js处理。
require("db333.php");
mysqli_query($conn,"set names 'utf8'"); //数据库输出编码
mysqli_select_db($conn,$mysql_database); //打开数据库
$result = mysqli_query($conn,"select * from cha2");//打开你的表
$data="";
$array= array();
for ($i=1; $i<=140; $i++) {
class User{
public $a[$i];
} }
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$user=new User();
for ($i=1; $i<=140; $i++) {
$user->a$i = $row['a$i'];
$array[]=$user;
}
$data=json_encode($array);
echo $data;
如果想for循环生成变量
如: $a1,$a2,$a3....
$name = "a"; //这一类的变量就是a1,a2····
for($i=0; $i<140; $i++){
${$a.$i} = $i; //把$a 和 $i 用{}括起来 然后前面放个$就OK啦
}
echo $a0; //0;
echo $a2; //2;
搞定了,谢谢
$len=140;
for($i=1;$i<=$len;$i++){
$ar[] = 'a'.$i;
}
for ($i=0; $i<$len; $i++) {
$user->{$ar[$i]} = $row[$ar[$i]];
}