Page 1 of 1

Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 9:30 am
by LonelyProgrammer
I am retrieving information from a remote site via their remote API call. The API call returns an associative array (a large one, that is) and takes about 30s to 1 minute to invoke. I am thinking of caching the associative array output, saving what you get when you do a print_r($array, true) to a flat file. The problem now is how to convert that text output back to an associative array.

Is there a way to take that output and parse it back into an associative array when I need it again?

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 9:31 am
by papa
Maybe a better way, but I would save it as an xml.

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 9:33 am
by jaoudestudios
If you want speed, csv or json. However, xml is more readable.

Can you give us an example of the file?

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 9:56 am
by LonelyProgrammer
Readability is not important here. The idea is just to cache the result (I probably will refresh it once per day) and pass the result on for further processing, hence the reason why I want is to be from array -> text -> array again.

This is how the output looks like

Code: Select all

 
{'hasAccessPass': 1, 
'userId': 349244, 
'starLevel': 2, 
'avatarName': 'Dusty', 
'isGuest': 0}
 
It's short by itself, but I need to make this call about 15 to 20 times for the page and it adds up.

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 10:05 am
by jaoudestudios
Is that the format you receive it in? Can you post all formats (array & text)

You could save the php array in a *.php file and just include the file, which will make the php array available to you.

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 12:08 pm
by LonelyProgrammer
After the API makes it call, it will return an array like this

Array ( [userId] => 6816329 [avatarName] => xxx [isGuest] => 0 [picUrl] => image.jpg [hasAccessPass] => 1 [accountCreationDate] => 2006-12-03 21:06:26 )

I have to make 15 to 20 calls to the API for a same page. So the idea is to save the array to a flat file and converting it back to an assocative array as above...

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 3:58 pm
by jaoudestudios
I would save it as a csv. Then it is one of the lowest common denominators, so in the future your options are open, you could always load it into another language or even a database table. Plus it is easy to work with :wink:

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 4:20 pm
by requinix
Surprised no one's mentioned serialization yet.
serialize to convert to a string, unserialize to convert back to an array.

Just for caching in PHP though: writing something in another language to parse that string is silly. And it's not guaranteed to be backwards-compatible if you upgrade PHP (though the odds are good).
Of the other options mentioned I like CSV (for simplicity) and XML (for flexibility).

Re: Saving the output of an assocative array and restoring it

Posted: Mon Jan 12, 2009 4:47 pm
by s.dot
Yeah, just serialize() the array. Store it in a database or flat file. When you need the array again just get the contents of the database field or flat file and run unserialize() on it.