最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php數(shù)組轉成json格式的方法

 更新時間:2015年03月09日 12:15:23   作者:panloon  
這篇文章主要介紹了php數(shù)組轉成json格式的方法,實例分析了php操作數(shù)組及json格式數(shù)據(jù)的方法,需要的朋友可以參考下

本文實例講述了php數(shù)組轉成json格式的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

復制代碼 代碼如下:
function array_to_json( $array ){
    if( !is_array( $array ) ){
        return false;
    }
    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){
        $construct = array();
        foreach( $array as $key => $value ){
            // We first copy each key/value pair into a staging array,
            // formatting each key and value properly as we go.
            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "'".addslashes($key)."'";
            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }
            // Add to staging array:
            $construct[] = "$key: $value";
        }
        // Then we collapse the staging array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";
    } else { // If the array is a vector (not associative):
        $construct = array();
        foreach( $array as $value ){
            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }
            // Add to staging array:
            $construct[] = $value;
        }
        // Then we collapse the staging array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }
    return $result;
}

希望本文所述對大家的php程序設計有所幫助。

相關文章

最新評論

竹北市| 太湖县| 贵州省| 眉山市| 保康县| 滨海县| 阿城市| 高陵县| 钦州市| 宜昌市| 万山特区| 来凤县| 泰和县| 海阳市| 崇信县| 梓潼县| 嘉义市| 普格县| 巴林右旗| 汝州市| 邵阳市| 峡江县| 偏关县| 积石山| 崇文区| 昌平区| 潮安县| 义马市| 塘沽区| 贞丰县| 威信县| 东宁县| 基隆市| 石渠县| 吉木乃县| 彰化市| 开封市| 高尔夫| 喀喇| 嘉黎县| 普兰店市|