如果还是在为:

1.  php中使用json_decode 显示NULL
2.  php获取的json数据中文乱码
3. 。。。。。。

这些问题而头疼,不妨使用璞玉写的处理json的函数:

function arrayToJson($array)
{
     arrayForRecursive($array, 'urlencode', true);
     $json = json_encode($array);
     $json = urldecode($json);
     return $json;
}

function arrayForRecursive(&$array, $function, $apply_to_keys_also=false)
{
     static $recursive_counter = 0;
     if (++$recursive_counter>1000)
          die('数组层次太深!');
     foreach ($array as $key=>$value)
     {
          if (is_array($value))
               arrayForRecursive($array[$key], $function, $apply_to_keys_also);
          else
               $array[$key] = $function(repalceSpecialSign($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--;
}

function struct2Array($item)
{
     if (!is_string($item))
     {
          $item = (array)$item;
          foreach ($item as $key=>$val)
          {
               $item[$key]  = Struct2Array($val);
          }
     }
     return $item;
}

/**
* repalceSpecialSign,替换特殊符号.
* @param str          string     一个字符串
* @return str
*/
function repalceSpecialSign($string)
{
     $string = preg_replace("/\s/","",$string);
     $string = str_replace("\\","\\\\",$string);
     $string = str_replace("\'","\\\'",$string);
     $string = str_replace("\"","\\\"",$string);
     $string = str_replace(PHP_EOL,'',$string);
     $string = str_replace("\n","",$string);
     $string = str_replace("\r","",$string);
     return $string;
}

调用方法:

     $arr = arrayToJson($arr);
     die($arr);

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="">

请选择吧!