Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Saving complex data structures to disk

    Hi guys,

    I would like to write a complex data structure (an object containing arrays of other objects) to disk. Unfortunately, file_put_contents() only accepts strings and arrays, so I can't use that.

    Does anybody know of a general purpose script which could accomplish this? Alternatively, does anybody know where I could look for one?

    Thanks very much.

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I've answered my own question. If I recursively cast each object as an array then I turn the entire structure into a an array...

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why not serialize them before writing them to the file/db, and then unserialize them after you read them from the file/db ?
    http://www.php.net/manual/nl/function.serialize.php
    http://www.php.net/manual/nl/function.unserialize.php
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool. I only thought serialize worked for arrays. This is just what I was looking for , thanks.

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you're welcome !
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there not a binary equivalent of serialize? Parsing/unparsing a big text file (mine is over 100k) looks quite expensive. Surely a structure could just be dumped from memory onto disk and then reloaded again, much like a binary swap file...

    Any thoughts?

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not sure what you are talking about.

    arrays and objects are 'in memory constructs' and serializing them is the most efficient way to get a storable representation of them. when you unserialize them, you immeadeately get an in memory construct. i don't realy understand what extra you can wish for.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #8
    New Coder
    Join Date
    May 2006
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm just pointing out that, in theory, it should be possible to dump the structure directly from memory to disk (without going through the process of serializing). This assumes that (i) the data structure occupies contiguous memory, (ii) there are no absolute references within the binary memory block.

    Basically, the only thing you could do with such a file is it load it back into memory - but thats all I'd want to do anyway.

    It was just a curiosity, serialize is working fine for me. I just have a feeling that the process could be speeded up...

    Tks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •