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

首頁 > 擴展 > JSON > 正文

php數組轉成json格式的方法

2024-09-06 20:18:14
字體:
來源:轉載
供稿:網友

 這篇文章主要介紹了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盱眙县| 农安县| 定南县| 大城县| 开平市| 洮南市| 孟村| 呼玛县| 滁州市| 扎鲁特旗| 太谷县| 兰州市| 石狮市| 宜宾市| 海门市| 珠海市| 青神县| 蒙城县| 开化县| 靖江市| 昌图县| 阳泉市| 萨迦县| 宁津县| 峨山| 莒南县| 木兰县| 冕宁县| 翼城县| 曲沃县| 西丰县| 庆元县| 福建省| 顺平县| 布拖县| 柘荣县| 新郑市| 深泽县| 呼和浩特市| 白玉县| 双牌县|