Storing a multidimensional array

Discussion in 'PHP' started by bloodredxxx, Aug 3, 2007.

  1. #1
    I'm trying to create a data cache for a SOAP server.

    How can I store a multidimensional array in a file (unknown dimensions).

    I understand you can use print_r() to display the contents/heirarchy of an array but how can I store it in a file?

    I was thinking of something like this :
    i want to store this variable $x in a php file so that I can just use include(); to load that variable. so i just do something like

    fwrite($file,'$x = '.$x);

    then use

    include($filename);

    whenever I want to load the variable.

    that, but I need to do it with multidimensional arrays.
     
    bloodredxxx, Aug 3, 2007 IP
  2. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The simplest way of doing this is with PHP's built-in serialize and unserialize functions.

    serialize will handle all kinds of data types for you, including multidimensional arrays, and create a string representation.
    However, instead of including it when trying to retrieve data, you'll need to use the unserialize function.

    Example:
    
    // To save data to file
    fwrite($file, serialize($array));
    
    // To read data back
    $array = unserialize(fread($file, <length of file>));
    
    PHP:
     
  3. bloodredxxx

    bloodredxxx Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks. That function was actually on my mind though I didn't know what it actually does... now I know. =)
     
    bloodredxxx, Aug 4, 2007 IP