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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松滋市| 磐安县| 怀远县| 金川县| 内丘县| 平凉市| 通山县| 尤溪县| 安仁县| 西昌市| 满城县| 锦屏县| 雅江县| 新宁县| 南漳县| 合江县| 扶余县| 甘德县| 文昌市| 福建省| 兴城市| 鹿邑县| 庆城县| 沛县| 谷城县| 龙里县| 轮台县| 松滋市| 潜山县| 乐昌市| 易门县| 肇庆市| 义乌市| 新丰县| 灵石县| 九龙坡区| 乐安县| 商丘市| 城固县| 剑川县| 琼海市|