国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玛曲县| 明水县| 宿迁市| 汝州市| 龙口市| 甘孜| 新营市| 辽宁省| 赣榆县| 林甸县| 武邑县| 彩票| 重庆市| 胶州市| 衡山县| 通化市| 南部县| 昌江| 济源市| 确山县| 清苑县| 突泉县| 临泉县| 方城县| 漠河县| 霍林郭勒市| 浙江省| 咸丰县| 广丰县| 宿州市| 文山县| 济源市| 金阳县| 岑溪市| 高安市| 洱源县| 外汇| 中西区| 阿坝| 麻栗坡县| 迁西县|