在for循环中创建Json数组-PHP [英] Create a Json array in a for loop - php
本文介绍了在for循环中创建Json数组-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?php
include '../connection.php';
$sql="SELECT userid,name,batch FROM dbusers";
$results=mysql_query($sql) or die("Cannot execute query");
$count=mysql_num_rows($results);
$arr=array();
for($i=0; $i < $count; $i++){
$rows=mysql_fetch_array($results);
//What to put here ?
}
json_encode($arr);
?>
这是我的php代码.我想问一下要放入for循环中的内容,以便可以在php中创建一个数组数组.内部数组将以userid,name和batch作为其元素.
This is my php code. I want to ask what to put inside the for loop so that I can create a an array of array in php. The inner array will have userid, name and batch as its elements .
推荐答案
在这里放什么?
$ arr [] = $ rows;
完整代码
<?php
include '../connection.php';
$sql="SELECT userid,name,batch FROM dbusers";
$results=mysql_query($sql) or die("Cannot execute query");
$count=mysql_num_rows($results);
$arr=array();
for($i=0; $i < $count; $i++){
$rows=mysql_fetch_array($results, MYSQL_ASSOC);//use MYSQL_ASSOC so you wouldn't have duplicate data
$arr[] = $rows;
}
$json = json_encode($arr);
?>
这篇关于在for循环中创建Json数组-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文