• php数组转为JSON字符串(兼容中文)


     /**************************************************************
         *
         *  将数组转换为JSON字符串(兼容中文)
         *  @param  array   $array      要转换的数组
         *  @return string      转换得到的json字符串
         *  @access public
         *
         *************************************************************/
        function JSON($array) {
            $this->arrayRecursive($array, 'urlencode', true);
            $json = json_encode($array);
            return urldecode($json);
        }
       /**************************************************************
         *
         *  使用特定function对数组中所有元素做处理
         *  @param  string  &$array     要处理的字符串
         *  @param  string  $function   要执行的函数
         *  @return boolean $apply_to_keys_also     是否也应用到key上
         *  @access public
         *
         *************************************************************/
        function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
        {
            static $recursive_counter = 0;
            if (++$recursive_counter > 1000) {
                die('possible deep recursion attack');
            }
            foreach ($array as $key => $value) {
                if (is_array($value)) {
                    $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
                } else {
                    $array[$key] = $function($value);
                }
    
                if ($apply_to_keys_also && is_string($key)) {
                    $new_key = $function($key);
                    if ($new_key != $key) {
                        $array[$new_key] = $array[$key];
                        unset($array[$key]);
                    }
                }
            }
            $recursive_counter--;
        }

    源码,亲测好用

  • 相关阅读:
    从Mysql中取出数据并用jieba统计词频
    关于important的用法
    Spring boot主启动类探究
    PHPMyadmin 配置多个网段
    Perl 对json的过滤脚本
    Perl 校验命中的脚本
    web渗透测试工具—Fiddler(转)
    PerlIde in NetBeans7.3 for Debian
    Perl last和next的用法区别
    2020.7.25
  • 原文地址:https://www.cnblogs.com/hanshuai0921/p/7928984.html
Copyright © 2020-2023  润新知