欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

php數(shù)組轉(zhuǎn)成json格式的方法_PHP教程

編輯Tag賺U幣
教程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返回字符串中所有單詞的方法。分享給大家供大家參考。具體分析如下: 這段代碼返回字符串中的所有單詞,

來(lái)源:模板無(wú)憂//所屬分類(lèi):PHP教程/更新時(shí)間:2015-03-10
相關(guān)PHP教程