从print_r输出中重新生成PHP数组。

问题描述 投票:16回答:2

比方说,我有一个来自某个源的输出,我无法访问PHP创建的原始数组。

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
        )

)

现在我想输入这些数据,然后让一个算法重新创建它所打印的原始数组,这样我就可以将其用于我自己的应用程序。

目前我正在考虑用一个 sub_str() 和regex语句来提取数据并将其适当放置. 在我继续前进之前,有没有更简单的方法,通过已经写好的代码或php插件来实现?

php arrays multidimensional-array output
2个回答
23
投票
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_r'Matt'是老板


0
投票

我也为你的问题研究出了一个解决方案,因为它是一个非常有用的工具,可以在Stack Overflow上重新创建问题。我的源码在这里。https:/github.cometalonaprp。

我的做法有点不同:在你的字符串中,你不需要换行,所以我在字符中穿行。有一个缺点:如果你的数组值中有括号或小括号,那就不行了。

© www.soinside.com 2019 - 2024. All rights reserved.