我使用php从mysql服务器获取一些数据并将其编码为json。
这是我到目前为止所做的:
[
{
"ImageID": "1",
"ImageName": "Hakuna Matata",
"Details": "some text"
},
{
"ImageID": "2",
"ImageName": "Hakuna Matata2",
"Details": "some text"
}
]
我想在"详细信息"中添加一个数组。但我不知道如何实现这一目标。就像这种格式一样:
[
{
"ImageID": "1",
"ImageName": "Hakuna Matata",
"Details": ["some text","some text"]
},
{
"ImageID": "2",
"ImageName": "Hakuna Matata2",
"Details": ["some text","some text"]
}
]
我是处理php的新手,所以请保持温柔:)
这是我的php文件:
<?php
$objConnect = mysql_connect("localhost","root","1234");
$objDB = mysql_select_db("weHappening");
$strSQL = "SELECT * FROM all_events WHERE 1 ";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($objConnect);
if (json_last_error() ===
JSON_ERROR_NONE){
//do something
echo json_encode($resultArray);
} else {
//error its not json
}
?>
答案 0 :(得分:0)
转到:
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
分为:
$field_name = mysql_field_name($objQuery, $i);
if ($field_name == 'Details') {
$value = [$objResult[$i]];
} else {
$value = $objResult[$i];
}
$arrCol[$field_name] = $value;
当然这没有经过测试,但你应该明白这一点。