[英]Get specific formatted JSON from PHP Query
I want to get an array like this: 我想得到一个像这样的数组:
{
"version": "1397992135_1932",
"list": {
"20514072":["John Carter","FiExJGIpsek.jpg",1],
"7247045":["Joe Satriani","KzvE54Z4rlA.jpg",0],
"91120813":["Mikel Arteta","JnPwkLKGeCA.jpg",1]
}
}
In database I have simple rows: 在数据库中我有简单的行:
id | name | photo | vip
How can I format result from query to what I want? 如何将查询结果格式化为我想要的?
Update 更新
I don't truly understand how to code this to fetch the data from DB if I have 100 records. 如果我有100条记录,我真的不明白如何编写这个来从DB中获取数据。
This would be the array setup required to achieve the JSON string you are looking for: 这将是实现您正在寻找的JSON字符串所需的数组设置:
$data = array(
"version" => "1397992135_1932",
"list" =>
array(
"20514072" => array(
"John Carter",
"iExJGIpsek.jpg",
"1"
),
"7247045" => array(
"Joe Satriani",
"KzvE54Z4rlA.jpg",
"0"
),
"91120813" => array(
"Mikel Arteta",
"JnPwkLKGeCA.jpg",
"1"
))
);
echo "<pre>";
echo json_encode($data);
echo "</pre>";
?>
OUTPUTS: 输出:
{
"version":"1397992135_1932",
"list":{
"20514072":["John Carter","iExJGIpsek.jpg","1"],
"7247045":["Joe Satriani","KzvE54Z4rlA.jpg","0"],
"91120813":["Mikel Arteta","JnPwkLKGeCA.jpg","1"]
}
}
OK, I found the solution for dynamic array =) 好的,我找到了动态数组的解决方案=)
$list = array();
while ($row = $result->fetch_array()) {
$list[$row['id']] = array($name,$photo,$vip);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.