/    Sign up×
Community /Pin to ProfileBookmark

Converting plaintext back to array.

I am currently printing an array (serialized and gzipped) to a page. I would like to convert the plaintext (uncompressed and unserialized) back into an array.

The array is multi-dimensional. An example of the array is below:

[code=php]Array
(
[test2] => Array
(
[0] => Array
(
[name] => iPod.png
[filesize] => 47224
[filename] => iPod.png
[md5] => f048ce6d5dd38236384ce6fe4729743f
[series] => test2
)

[1] => Array
(
[name] => song.txt
[filesize] => 696
[filename] => song.txt
[md5] => 1ad394e40df12e63432e0d24eff8c2f1
[series] => test2
)

[2] => Array
(
[name] => winteryawn.jpg
[filesize] => 22977
[filename] => winteryawn.jpg
[md5] => a3c6840a188bfb9cfbc13c6854a42f61
[series] => test2
)

[3] => Array
(
[name] => raidz.png
[filesize] => 7854
[filename] => raidz.png
[md5] => 02fd1c918491276c29969034970ddb98
[series] => test2
)

)

[test] => Array
(
[0] => Array
(
[name] => iPod.png
[filesize] => 47224
[filename] => iPod.png
[md5] => f048ce6d5dd38236384ce6fe4729743f
[series] => test
)

[1] => Array
(
[name] => song.txt
[filesize] => 696
[filename] => song.txt
[md5] => 1ad394e40df12e63432e0d24eff8c2f1
[series] => test
)

[2] => Array
(
[name] => files.php
[filesize] => 2731
[filename] => files.php
[md5] => 8a0619677a72695d014fe78d50a63163
[series] => test
)

[3] => Array
(
[name] => winteryawn.jpg
[filesize] => 22977
[filename] => winteryawn.jpg
[md5] => a3c6840a188bfb9cfbc13c6854a42f61
[series] => test
)

[4] => Array
(
[name] => raidz.png
[filesize] => 7854
[filename] => raidz.png
[md5] => 02fd1c918491276c29969034970ddb98
[series] => test
)

)

)[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@cridleySep 14.2006 — what's the plaintext look like? looks like xml would be a good format for it...
Copy linkTweet thisAlerts:
@neenach2002authorSep 14.2006 — what's the plaintext look like? looks like xml would be a good format for it...[/QUOTE]
I posted the plaintext...?

Here it is again:

[code=php]Array
(
[test2] => Array
(
[0] => Array
(
[name] => iPod.png
[filesize] => 47224
[filename] => iPod.png
[md5] => f048ce6d5dd38236384ce6fe4729743f
[series] => test2
)

[1] => Array
(
[name] => song.txt
[filesize] => 696
[filename] => song.txt
[md5] => 1ad394e40df12e63432e0d24eff8c2f1
[series] => test2
)

[2] => Array
(
[name] => winteryawn.jpg
[filesize] => 22977
[filename] => winteryawn.jpg
[md5] => a3c6840a188bfb9cfbc13c6854a42f61
[series] => test2
)

[3] => Array
(
[name] => raidz.png
[filesize] => 7854
[filename] => raidz.png
[md5] => 02fd1c918491276c29969034970ddb98
[series] => test2
)

)

[test] => Array
(
[0] => Array
(
[name] => iPod.png
[filesize] => 47224
[filename] => iPod.png
[md5] => f048ce6d5dd38236384ce6fe4729743f
[series] => test
)

[1] => Array
(
[name] => song.txt
[filesize] => 696
[filename] => song.txt
[md5] => 1ad394e40df12e63432e0d24eff8c2f1
[series] => test
)

[2] => Array
(
[name] => files.php
[filesize] => 2731
[filename] => files.php
[md5] => 8a0619677a72695d014fe78d50a63163
[series] => test
)

[3] => Array
(
[name] => winteryawn.jpg
[filesize] => 22977
[filename] => winteryawn.jpg
[md5] => a3c6840a188bfb9cfbc13c6854a42f61
[series] => test
)

[4] => Array
(
[name] => raidz.png
[filesize] => 7854
[filename] => raidz.png
[md5] => 02fd1c918491276c29969034970ddb98
[series] => test
)

)

)
[/code]
Copy linkTweet thisAlerts:
@NogDogSep 14.2006 — I suppose you could write a function to parse the text line by line, look for values in brackets, note indent level, etc., etc., and eventually build an array from the textual data; but, I would start by asking: why are you going through these evolutions?

My guess is that by understanding your functional requirements that led you to this (rather strange to me) sequence of events, we might be able to suggest a simpler, cleaner, and easier solution to your needs.
Copy linkTweet thisAlerts:
@neenach2002authorSep 14.2006 — Well, I couldn't get the function to work (from the other thread I posted).

So what I'm doing is a file_get_contents on the file. The file itself is serializing then gzdeflating the output. The file reading the contents inflates and unserializes.

I was thinking of using explode then implode. Is there a way to just use serialize to get it back into an accessible array?
Copy linkTweet thisAlerts:
@NogDogSep 14.2006 — I'm still confused as to what you are doing. If you're trying to save an array to a file and then read it back into an array at a later time, you should be able to simply do:
[code=php]
// save array to file:
$myArray = array([array definition here]);
$fh = fopen("somefile", "w");
fwrite($fh, serialize($myArray));
fclose($fh);

// get array from file
$serialized = file_get_contents("somefile");
if($serialized !== FALSE)
{
$myArrayCopy = unserialize($serialized);
}
else
{
// handle error here
}
[/code]

So what is it I'm not understanding about what you want to do?
Copy linkTweet thisAlerts:
@bokehSep 14.2006 — If you used var_export rather than print_r to produce the plaintext file the array would already be in a usable form.
Copy linkTweet thisAlerts:
@neenach2002authorSep 14.2006 — NogDog, the problem is I'm not saving it to a file. However it looks like the second half of what you posted will work.

Bokeh, I want to serialize the array. I can't serialize it using var_export =(
×

Success!

Help @neenach2002 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...