Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Let's say I have this output from some source where I don't have access to the original PHP created array:

Array
(
    [products] => Array
        (
            [name] => Arduino Nano Version 3.0 mit ATMEGA328P
            [id] => 10005
        )

    [listings] => Array
        (
            [category] => 
            [title] => This is the first line
This is the second line
            [subtitle] => This is the first subtitle
This is the second subtitle
            [price] => 24.95
            [quantity] => 
            [stock] => 
            [shipping_method] => Slow and cheap
            [condition] => New
            [defects] => 
        )

    [table_count] => 2
    [tables] => Array
        (
            [0] => products
            [1] => listings
        )

)

Now I would like to input that data and have an algorithm recreate the original array that it was printing so I can then use it for my own application.

Currently I am thinking about a sub_str() and regex statements that pull data and place it appropriately. Before I head further, is there a simpler way, via already written code or php plugins that do this for me already out there?

share|improve this question
    
show what you've tried... – michi Apr 25 '13 at 22:55
    
Currently i am thinking about a sub_str() and regex statements that pull data and place it appropriately. Before i head further, is there a simpler way, via already written code or php plugins that do this for me already out there? – Dan Apr 25 '13 at 22:56
3  
this is what var_export is for, instead of print_r. – Jonathan Kuhn Apr 25 '13 at 23:00
1  
Also, check the user comments on php.net for print_r. There are a couple reverse functions that have already been written. – Jonathan Kuhn Apr 25 '13 at 23:06
1  
You can write some basic parser for it, but as values aren't delimited, you may end up with a different array then the original, the print_r output was never meant to be read back in. (for instance, when => Array( occurs in a value itself. Rare perhaps, but a consideration. – Wrikken Apr 25 '13 at 23:07

2 Answers 2

up vote 2 down vote accepted

this works tasty: thank you Jonathan Kuhn

function print_r_reverse($in) {
    $lines = explode("\n", trim($in));
    if (trim($lines[0]) != 'Array') {
        // bottomed out to something that isn't an array
        return $in;
    } else {
        // this is an array, lets parse it
        if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
            // this is a tested array/recursive call to this function
            // take a set of spaces off the beginning
            $spaces = $match[1];
            $spaces_length = strlen($spaces);
            $lines_total = count($lines);
            for ($i = 0; $i < $lines_total; $i++) {
                if (substr($lines[$i], 0, $spaces_length) == $spaces) {
                    $lines[$i] = substr($lines[$i], $spaces_length);
                }
            }
        }
        array_shift($lines); // Array
        array_shift($lines); // (
        array_pop($lines); // )
        $in = implode("\n", $lines);
        // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
        preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        $pos = array();
        $previous_key = '';
        $in_length = strlen($in);
        // store the following in $pos:
        // array with key = key of the parsed array's item
        // value = array(start position in $in, $end position in $in)
        foreach ($matches as $match) {
            $key = $match[1][0];
            $start = $match[0][1] + strlen($match[0][0]);
            $pos[$key] = array($start, $in_length);
            if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
            $previous_key = $key;
        }
        $ret = array();
        foreach ($pos as $key => $where) {
            // recursively see if the parsed out value is an array too
            $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
        }
        return $ret;
    }
} 

not my code, found here in the comments: print_r 'Matt' is the owner

share|improve this answer

I also worked out a solution for your problem because it is a very useful tool to recreate issues here on stackoverflow. My source is located here: https://github.com/etalon/aprp.git

I did it a bit different: In your string you don't need line-breaks, so I walk through the chars. One disatvantage: If there are brackets or paranthesises in your array-values it won't work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.