Home  >  Article  >  Backend Development  >  Introduction to constructing JSON and new arrays from PHP data sets_PHP tutorial

Introduction to constructing JSON and new arrays from PHP data sets_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:38840browse

Friends who need an introduction to the JSON and new array construction of PHP data sets can refer to it.

          $jstr='['; ​​​​while($rs = $recordset->Fetch())

The code is as follows
 代码如下 复制代码

function RecordToJson($recordset)
    {
        $jstr='[';
        while($rs = $recordset->Fetch())
        {
            //$nick = iconv("GBK",'utf-8',$rs['nick']);/*转换为utf-8编码*/
            //TODO:遍历结果集
            $arr_keys=array_keys($rs);
            $jstr=$jstr.'{';
            for($i=0;$i {
//数据库编码为gbk,需要转换编码
//TODO;iconv("GBK",'utf-8',$rs['nick']);/*转换为utf-8编码*/
$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;
}

Copy code


function RecordToJson($recordset)
代码如下 复制代码


function RebuilderRecord($recordset)
{
$row=0;
while($rs = $recordset->Fetch())
        {
            //TODO:遍历结果集
            $arr_keys=array_keys($rs);
            for($i=0;$i {
$newrs[$row][$arr_keys[$i]]=$rs[$arr_keys[$i]];
}
$row++;
}
return $newrs;
}

{
            { //$nick = iconv("GBK",'utf-8',$rs['nick']);/*Convert to utf-8 encoding*/ //TODO: Traverse the result set                  $arr_keys=array_keys($rs);                  $jstr=$jstr.'{'; for($i=0;$i                  {<🎜> //The database encoding is gbk, the encoding needs to be converted<🎜> //TODO;iconv("GBK",'utf-8',$rs['nick']);/*Convert to utf-8 encoding*/<🎜>                   $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;<🎜> }<🎜> <🎜> <🎜> <🎜> <🎜><🎜> PHP's default result set array has a numeric index. The following function can remove the numeric index and only retain the field index: <🎜>
The code is as follows<🎜> Copy code<🎜> <🎜>
<🎜><🎜> function RebuilderRecord($recordset)<🎜> {<🎜>          $row=0;<🎜> ​​​​while($rs = $recordset->Fetch())             { //TODO: Traverse the result set                  $arr_keys=array_keys($rs); for($i=0;$i

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631587.htmlTechArticlePHP data set construction JSON and new array introduction. Friends in need can refer to it. The code is as follows Copy code function RecordToJson($recordset) { $jstr='['; while($rs = $recordset-Fe...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn