serialize a large array in PHP?

前端 未结 13 1947
死守一世寂寞
死守一世寂寞 2021-01-04 06:11

I am curious, is there a size limit on serialize in PHP. Would it be possible to serialize an array with 5,000 keys and values so it can be stored into a cache?

I am

13条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 06:44

    There is no limit, but remember that serialization and unserialization has a cost.

    Unserialization is exteremely costly.

    A less costly way of caching that data would be via var_export() as such (since PHP 5.1.0, it works on objects):

    $largeArray = array(1,2,3,'hello'=>'world',4);
    
    file_put_contents('cache.php', "

    You can then simply retrieve the array by doing the following:

    $largeArray = include('cache.php');
    

    Resources are usually not cache-able.

    Unfortunately, if you have circular references in your array, you'll need to use serialize().

提交回复
热议问题