Re: Converting print_r() output to an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Sep 30, 2009 at 23:07, James Colannino <james@xxxxxxxxxxxxx> 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?  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).

    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:

<?php
$sentence = "This is a test.";
$words = explode(' ',$sentence);
/*
Then $words would contain:
    [0] => This
    [1] => is
    [2] => a
    [3] => 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:

<?php
// From $words in the previous example....
shuffle($words);
$sentence = 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.

-- 
</Daniel P. Brown>
daniel.brown@xxxxxxxxxxxx || danbrown@xxxxxxx
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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux