Converting print_r() output to an array
Converting print_r() output to an array
am 01.10.2009 05:07:54 von James Colannino
Hey everyone, I was pretty sure there was an easy built-in solution for
what I want to do, but I've been googling around with no luck.
Basically, I just want to take a string containing the output of
print_r() and convert it back into an array again.
That is possible, right? If so, how do I go about it? If not, what's a
quick and easy way to parse a string and turn it into an array (I don't
necessarily need the string to be in the format print_r returns).
Thanks!
James
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting print_r() output to an array
am 01.10.2009 05:12:02 von mike503
first off, if you pass print_r($var, true) it will return it instead
of printing it. if you go that route.
have you looked at var_export() ?
On Wed, Sep 30, 2009 at 8:07 PM, James Colannino wrot=
e:
> Hey everyone, I was pretty sure there was an easy built-in solution for
> what I want to do, but I've been googling around with no luck.
> Basically, I just want to take a string containing the output of
> print_r() and convert it back into an array again.
>
> That is possible, right? Â If so, how do I go about it? Â If not,=
what's a
> quick and easy way to parse a string and turn it into an array (I don't
> necessarily need the string to be in the format print_r returns).
>
> Thanks!
>
> James
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting print_r() output to an array
am 01.10.2009 05:16:20 von Jonathan Tapicer
Hi,
May be you want to take a look at serialize and unserialize functions,
serialize generates a string from a variable and then unserialize can
give you the value of the variable back from the string.
Regards,
Jonathan
On Thu, Oct 1, 2009 at 12:07 AM, James Colannino wrot=
e:
> Hey everyone, I was pretty sure there was an easy built-in solution for
> what I want to do, but I've been googling around with no luck.
> Basically, I just want to take a string containing the output of
> print_r() and convert it back into an array again.
>
> That is possible, right? =A0If so, how do I go about it? =A0If not, what'=
s a
> quick and easy way to parse a string and turn it into an array (I don't
> necessarily need the string to be in the format print_r returns).
>
> Thanks!
>
> James
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting print_r() output to an array
am 01.10.2009 05:28:26 von Daniel Brown
On Wed, Sep 30, 2009 at 23:07, James Colannino wrote:
> Hey everyone, I was pretty sure there was an easy built-in solution for
> what I want to do, but I've been googling around with no luck.
> Basically, I just want to take a string containing the output of
> print_r() and convert it back into an array again.
Well, print_r() simply iterates the array and prints it.... so if
you just want the array, skip print_r() entirely and use the variable
you were passing to it.
> That is possible, right? =A0If so, how do I go about it? =A0If not, what'=
s a
> quick and easy way to parse a string and turn it into an array (I don't
> necessarily need the string to be in the format print_r returns).
There's plenty of ways to parse strings into arrays, it just
depends on how you want to do it. For one example, if you wanted to
split each word into an individual array value, you could do:
$sentence =3D "This is a test.";
$words =3D explode(' ',$sentence);
/*
Then $words would contain:
[0] =3D> This
[1] =3D> is
[2] =3D> a
[3] =3D> test.
*/
?>
Then, say you wanted to shuffle the words around, and then convert
them back to an array. I don't know why, just say it.... out loud.
I'll wait.
Nevermind, here's the example anyway:
// From $words in the previous example....
shuffle($words);
$sentence =3D implode(' ',$words);
?>
The latter example will mix up the words and put them back into a
sentence, with a whitespace (' ') as the "glue" between them.
--=20
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php