php数组转成json格式的方法
2015-03-09 12:15
543 查看
复制代码 代码如下:
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程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- PHP数据集构建JSON格式及新数组的方法
- php 将json格式数据转换成数组的方法
- PHP数据集构建JSON格式及新数组的方法
- PHP <5.2ver 将数组转换成json格式的方法
- php数组转成json格式的方法
- php里少用到的session_module_name,以及session的key值限制,简单将session存储为json格式数据的方法
- 两种方法在Django框架中支持后台返回包含中文的JSON/数组格式
- js/jquery解析json和数组格式的方法详解
- js/jquery解析json和数组格式的方法详解
- php下哪种方法合适解析xml,转换成json格式
- json和php数组 格式的互相转换
- PHP-PHP5.3及以上版本中检查json格式的方法
- extjs Post方法提交json格式的数据到php
- php将数组转换成csv格式文件输出的方法
- PHP JSON格式的中文显示问题解决方法
- PHP中使用json数据格式定义字面量对象的方法
- PHP用json_encode()将数组里面带有中文的字串保留以JSON格式返回
- PHP 生成复杂JSON格式 简单快速方法
- php判断是否为json格式的方法
- php判断是否为json格式的方法