Home > php教程 > php手册 > body text

php使用json代替serialize

WBOY
Release: 2016-05-25 16:47:21
Original
911 people have browsed it

php使用json代替serialize,有需要的朋友可参考一下.

需要注意的是 json_decode时返回的是object,需要json_decode("字符串",ture);后来发现也不是那么完美..

json_encode会对中文进行编码.如果含有大量的中文,那长度超过了serialize.这让人很蛋疼,网上给出的办法是对内容进行urlencode,json_enocde后再urldecode

实例代码如下:

<?php
function jsonencode($code) { //新json_encode
    $code = json_encode(urlencodearray($code)); //对数组处理
    return urldecode($code);
}
function urlencodearray($data) { //urlencode数组
    if (is_array($data)) {
        foreach ($data as $key => $val) {
            $data[$key] = urlencodearray($val);
        }
        return $data;
    } else {
        return urlencode($data);
    }
}
?>
Copy after login


教程地址:

欢迎转载!但请带上文章地址^^

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!