How do I store an array in a file to access as an array later with PHP?

前端 未结 8 1345
粉色の甜心
粉色の甜心 2020-12-02 14:17

I just want to quickly store an array which I get from a remote API, so that i can mess around with it on a local host.

So:

  1. I currently have an array.<
相关标签:
8条回答
  • 2020-12-02 14:53

    If you don't need the dump file to be human-readable, you can just serialize() the array.

    storing:

    file_put_contents('yourfile.bin', serialize($array));
    

    retrieving:

    $array = unserialize(file_get_contents('yourfile.bin'));
    
    0 讨论(0)
  • 2020-12-02 14:54

    Talking about php use, for performance sake, avoid encoding and decoding everything, just save array with:

     file_put_contents('dic.php', "<?php \n".'$dic='.var_export($dic, true).';');
    

    and call normally with

    include "dic.php";
    
    0 讨论(0)
提交回复
热议问题