Revert output from print_r to its original PHP array

2

This question I ask because of a difficulty that I find especially answering questions right here.

When sharing the result of print_r of an array, for example:

Array
(
    [0] => Array
        (
            [dim1] => Array
                (
                    [dim1.1] => valor1.1
                    [dim1.2] => valor1.2
                )

        )

    [1] => Array
        (
            [dim2] => Array
                (
                    [dim2.1] => valor2.1
                    [dim2.2] => valor2.2
                    [dim2.3] => valor2.3
                )

        )

)

Interesting to have the original array, for example to do code tests.

Is there a proper PHP function to revert the original array from print_r regardless of the structure or dimensions of the array?

Is there any other way to do it?

For example:

The result you want to obtain from the print_r indicated above would be a variable like this:

$array=array(
                array("dim1"=>array ("dim1.1"=>"valor1.1", "dim1.2"=>"valor1.2")), 
                array("dim2"=>array ("dim2.1"=>"valor2.1", "dim2.2"=>"valor2.2", "dim2.3"=>"valor2.3"))
            );
  

But, it's about finding a function that converts any    print_r output to your original array .

    
asked by A. Cedano 18.05.2018 в 01:32
source

1 answer

1
  

Is there a proper PHP function to revert the original array from a print_r regardless of the structure or dimensions of the array?

At least I do not know her.

  

Is there any other way to do it?

This code I found in the php manual written by Matt . You can see it working in PHP Sandbox .

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; 
    } 
}

$print = "Array
(
    [0] => Array
        (
            [dim1] => Array
                (
                    [dim1.1] => valor1.1
                    [dim1.2] => valor1.2
                )

        )

    [1] => Array
        (
            [dim2] => Array
                (
                    [dim2.1] => valor2.1
                    [dim2.2] => valor2.2
                    [dim2.3] => valor2.3
                )

        )

)";

$result = print_r_reverse($print);

var_dump($result);
  

I have not thoroughly tested it, but the example you have indicated resolves it correctly.

    
answered by 18.05.2018 в 09:47