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

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

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

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

復(fù)制代碼 代碼如下:
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程序設(shè)計有所幫助。

相關(guān)文章

最新評論

徐闻县| 崇仁县| 璧山县| 泸定县| 惠安县| 林州市| 南宫市| 宜兰市| 商洛市| 武胜县| 仪陇县| 茶陵县| 拜城县| 湛江市| 贵德县| 宿州市| 都匀市| 旬阳县| 育儿| 文水县| 怀集县| 宽城| 满洲里市| 文登市| 西贡区| 乌拉特前旗| 深圳市| 双辽市| 库尔勒市| 富民县| 沙田区| 宁海县| 永寿县| 杨浦区| 彰化县| 兴国县| 灵宝市| 濮阳市| 大宁县| 武夷山市| 宣化县|