php數(shù)組轉(zhuǎn)成json格式的方法
更新時間:2015年03月09日 12:15:23 作者:panloon
這篇文章主要介紹了php數(shù)組轉(zhuǎn)成json格式的方法,實例分析了php操作數(shù)組及json格式數(shù)據(jù)的方法,需要的朋友可以參考下
本文實例講述了php數(shù)組轉(zhuǎn)成json格式的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
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;
}
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程序設(shè)計有所幫助。
您可能感興趣的文章:
- php curl獲取到j(luò)son對象并轉(zhuǎn)成數(shù)組array的方法
- php實現(xiàn)數(shù)組中索引關(guān)聯(lián)數(shù)據(jù)轉(zhuǎn)換成json對象的方法
- JSON PHP中,Json字符串反序列化成對象/數(shù)組的方法
- 淺析C/C++,Java,PHP,JavaScript,Json數(shù)組、對象賦值時最后一個元素后面是否可以帶逗號
- php數(shù)組轉(zhuǎn)換js數(shù)組操作及json_encode的用法詳解
- php 將json格式數(shù)據(jù)轉(zhuǎn)換成數(shù)組的方法
- PHP實現(xiàn)數(shù)組轉(zhuǎn)JSon和JSon轉(zhuǎn)數(shù)組的方法示例
- php json轉(zhuǎn)換成數(shù)組形式代碼分享
- PHP自定義遞歸函數(shù)實現(xiàn)數(shù)組轉(zhuǎn)JSON功能【支持GBK編碼】
- PHP數(shù)組對象與Json轉(zhuǎn)換操作實例分析
相關(guān)文章
PHP 使用MySQL管理Session的回調(diào)函數(shù)詳解
本篇文章文章是對PHP中使用MySQL管理Session的回調(diào)函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Linux服務(wù)器下PHPMailer發(fā)送郵件失敗的問題解決
這篇文章主要給大家介紹了關(guān)于如何解決Linux服務(wù)器下PHPMailer發(fā)送郵件失敗的問題,文中通過示例代碼介紹的很詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03仿AS3實現(xiàn)PHP 事件機(jī)制實現(xiàn)代碼
仿AS3實現(xiàn)PHP 事件機(jī)制實現(xiàn)代碼,需要的朋友可以參考下。2011-01-01php連接函數(shù)implode與分割explode的深入解析
本篇文章是對php連接函數(shù)implode與分割explode進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06