国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php數組轉成json格式的方法

2024-05-04 23:32:08
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了php數組轉成json格式的方法,實例分析了php操作數組及json格式數據的方法,需要的朋友可以參考下
 

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

 

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浮梁县| 萨迦县| 南安市| 兴国县| 江西省| 莱阳市| 永定县| 阿尔山市| 苏尼特右旗| 台湾省| 团风县| 安达市| 达州市| 任丘市| 平乐县| 丰台区| 分宜县| 德庆县| 东港市| 清苑县| 团风县| 高阳县| 金溪县| 合水县| 贵德县| 南江县| 忻城县| 镇坪县| 革吉县| 仁怀市| 夹江县| 中西区| 建瓯市| 阳高县| 四川省| 兰坪| 霍州市| 邵阳县| 梁平县| 青川县| 沈丘县|