zoukankan      html  css  js  c++  java
  • 在php4下可用,简单的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;
    }
  • 相关阅读:
    SQL Server的链接服务器技术小结
    关于ACCESS的日期类型字段比较的一点认识
    Oracle与SQL Server的互连
    有关自定义消息广播 SendMessage(HWND_BROADCAST,WM_MyMsg,0,0);
    呵呵,不错,在这安家啦
    jquery常用验证
    Sql事务
    Jquery解析XML文件
    ASP.NET页面之间传递值的几种方式
    Yahoo,Msn,Skype,QQ,阿里旺旺在线聊天链接接口调用
  • 原文地址:https://www.cnblogs.com/qhorse/p/4597161.html
Copyright © 2011-2022 走看看