PHP data set method for building JSON format and a new array
- 2020-05-27 04:28:22
- OfStack
I wrote an PHP result set and converted it into an JSON function, which can be directly called:
The default result set group of PHP has a numeric index. The following function can remove the numeric index and only keep the field index:
function RecordToJson($recordset)
{
$jstr='[';
while($rs = $recordset->Fetch())
{
//$nick = iconv("GBK",'utf-8',$rs['nick']);/* convert utf-8 coding */
//TODO: Traverse the result set
$arr_keys=array_keys($rs);
$jstr=$jstr.'{';
for($i=0;$i<count($arr_keys);$i+=2)
{
// The database code is gbk , the encoding needs to be converted
//TODO;iconv("GBK",'utf-8',$rs['nick']);/* convert utf-8 coding */
$key=iconv("GBK",'utf-8',$arr_keys[$i]);//$arr_keys[$i];
$value=iconv("GBK",'utf-8',$rs[$arr_keys[$i]]);//$rs[$arr_keys[$i]];
$jstr=$jstr.'"'.$key.'":"'.$value.'",';
}
$jstr=substr($jstr,0,strlen($jstr)-1);
$jstr=$jstr.'},';
}
$jstr=substr($jstr,0,strlen($jstr)-1);
$jstr=$jstr.']';
return $jstr;
}
The default result set group of PHP has a numeric index. The following function can remove the numeric index and only keep the field index:
function RebuilderRecord($recordset)
{
$row=0;
while($rs = $recordset->Fetch())
{
//TODO: Traverse the result set
$arr_keys=array_keys($rs);
for($i=0;$i<count($arr_keys);$i+=2)
{
$newrs[$row][$arr_keys[$i]]=$rs[$arr_keys[$i]];
}
$row++;
}
return $newrs;
}