Saving the output of an assocative array and restoring it
Moderator: General Moderators
-
LonelyProgrammer
- Forum Contributor
- Posts: 108
- Joined: Sun Oct 12, 2003 7:10 am
Saving the output of an assocative array and restoring it
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?
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.
Re: Saving the output of an assocative array and restoring it
Maybe a better way, but I would save it as an xml.
- 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
If you want speed, csv or json. However, xml is more readable.
Can you give us an example of the file?
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
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
It's short by itself, but I need to make this call about 15 to 20 times for the page and it adds up.
This is how the output looks like
Code: Select all
{'hasAccessPass': 1,
'userId': 349244,
'starLevel': 2,
'avatarName': 'Dusty',
'isGuest': 0}
- 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
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.
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
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...
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...
- 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
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 
Re: Saving the output of an assocative array and restoring it
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).
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
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.