栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

存储PHP数组的首选方法(json_encode与序列化)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

存储PHP数组的首选方法(json_encode与序列化)

取决于您的优先级。

如果性能是您的绝对驾驶特性,那么请务必使用最快的一种。做出选择之前,只需确保您对差异有充分的了解

  • 不同于
    serialize()
    您需要添加额外的参数来保持UTF-8字符不变:(
    json_enpre($array, JSON_UNESCAPED_UNICODE)
    否则,它将UTF-8字符转换为Unipre转义序列)。
  • JSON将不存储该对象的原始类是什么(它们总是作为stdClass的实例还原)。
  • 你不能充分利用
    __sleep()
    ,并
    __wakeup()
    与JSON
  • 默认情况下,仅公共属性使用JSON序列化。(
    PHP>=5.4
    您可以实现JsonSerializable来更改此行为)。
  • JSON更可移植

目前可能还没有想到其他一些差异。

一个简单的速度测试来比较两者

<?phpini_set('display_errors', 1);error_reporting(E_ALL);// Make a big, honkin test array// You may need to adjust this depth to avoid memory limit errors$testArray = fillArray(0, 5);// Time json encoding$start = microtime(true);json_enpre($testArray);$jsonTime = microtime(true) - $start;echo "JSON enpred in $jsonTime secondsn";// Time serialization$start = microtime(true);serialize($testArray);$serializeTime = microtime(true) - $start;echo "PHP serialized in $serializeTime secondsn";// Compare themif ($jsonTime < $serializeTime) {    printf("json_enpre() was roughly %01.2f%% faster than serialize()n", ($serializeTime / $jsonTime - 1) * 100);}else if ($serializeTime < $jsonTime ) {    printf("serialize() was roughly %01.2f%% faster than json_enpre()n", ($jsonTime / $serializeTime - 1) * 100);} else {    echo "Impossible!n";}function fillArray( $depth, $max ) {    static $seed;    if (is_null($seed)) {        $seed = array('a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10);    }    if ($depth < $max) {        $node = array();        foreach ($seed as $key) { $node[$key] = fillArray($depth + 1, $max);        }        return $node;    }    return 'empty';}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号