Saving the output of an assocative array and restoring it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Saving the output of an assocative array and restoring it

Post 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?
Last edited by LonelyProgrammer on Mon Jan 12, 2009 9:31 am, edited 1 time in total.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

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

Post by papa »

Maybe a better way, but I would save it as an xml.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post by jaoudestudios »

If you want speed, csv or json. However, xml is more readable.

Can you give us an example of the file?
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

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

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post 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.
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

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

Post 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...
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post 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:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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).
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply