search
Homephp教程php手册PHP数据集构建JSON及新数组介绍

PHP数据集构建JSON及新数组介绍

Jun 13, 2016 am 10:01 AM
jsonphpintroducedataarraynew arrayConstruct

PHP数据集构建JSON及新数组介绍介绍有需要的朋友可参考一下。

 代码如下 复制代码

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;
    }


PHP默认的结果集数组有数字索引,下面函数可以去除数字索引,只保留字段索引:

 代码如下 复制代码


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;
    }

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.