php數(shù)組轉(zhuǎn)成json格式的方法_PHP教程
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!
推薦:php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法這篇文章主要介紹了php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法,實(shí)例分析了php操作數(shù)組及XML格式文件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法。分享給大家供大家參考。具體如下: 1. php代碼如下: ? 2. 運(yùn)行效果如下圖所示:
這篇文章主要介紹了php數(shù)組轉(zhuǎn)成json格式的方法,實(shí)例分析了php操作數(shù)組及json格式數(shù)據(jù)的方法,需要的朋友可以參考下
本文實(shí)例講述了php數(shù)組轉(zhuǎn)成json格式的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
代碼如下: 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;
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
分享:php返回字符串中所有單詞的方法這篇文章主要介紹了php返回字符串中所有單詞的方法,實(shí)例分析了php字符串正則匹配與數(shù)組操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了php返回字符串中所有單詞的方法。分享給大家供大家參考。具體分析如下: 這段代碼返回字符串中的所有單詞,
相關(guān)PHP教程:
- php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法
- php返回字符串中所有單詞的方法
- php通過(guò)正則表達(dá)式記取數(shù)據(jù)來(lái)讀取xml的方法
- PHP實(shí)現(xiàn)算式驗(yàn)證碼和漢字驗(yàn)證碼實(shí)例
- PHP實(shí)現(xiàn)指定字段的多維數(shù)組排序函數(shù)分享
- PHP多線程之內(nèi)部多線程實(shí)例分析
- php建立Ftp連接的方法
- Thinkphp調(diào)用Image類(lèi)生成縮略圖的方法
- PHP實(shí)現(xiàn)懶加載的方法
- php根據(jù)某字段對(duì)多維數(shù)組進(jìn)行排序的方法
- PHP多線程編程之管道通信實(shí)例分析
- PHP查詢快遞信息的方法
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP命名空間(Namespace)的使用詳解
- 關(guān)于二級(jí)目錄拖拽排序的實(shí)現(xiàn)(源碼示例下載)
- 如何使用PHP調(diào)用TinyURL API
- 基于PHP Web開(kāi)發(fā)MVC框架的Smarty使用說(shuō)明
- PHP調(diào)用MsSQL Server 2012存儲(chǔ)過(guò)程獲取多結(jié)果集(包含output參數(shù))的詳解
- PHP面向?qū)ο蟆獑卫J?/a>
- 解析PHP中empty is_null和isset的測(cè)試
- PHP錯(cuò)誤:Forbidden You don't have permission to access / on this server
- 壞狼的PHP學(xué)習(xí)第1天!
- 在同一窗體中使用PHP來(lái)處理多個(gè)提交任務(wù)
- 相關(guān)鏈接:
- 教程說(shuō)明:
PHP教程-php數(shù)組轉(zhuǎn)成json格式的方法。