【问题标题】:Recreate original PHP array from print_r output [duplicate]从 print_r 输出重新创建原始 PHP 数组 [重复]
【发布时间】:2013-04-20 00:21:39
【问题描述】:

假设我从无法访问原始 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() 和正则表达式来提取数据并适当地放置它。在我进一步之前,是否有更简单的方法,通过已经编写的代码或 php 插件来为我做这件事?

它必须适用于多数组 - 所以这篇文章不起作用,它甚至引用了正确的函数来使用: How create an array from the output of an array printed with print_r?

【问题讨论】:

  • 显示你的尝试...
  • 目前我正在考虑一个 sub_str() 和 regex 语句来提取数据并适当地放置它。在我更进一步之前,是否有更简单的方法,通过已经编写的代码或 php 插件可以为我做到这一点?
  • 这是 var_export 的用途,而不是 print_r。
  • 另外,检查 php.net 上的用户 cmets 是否有 print_r。已经编写了几个反向函数。
  • 你可以为它写一些基本的解析器,但是由于值没有分隔,你可能会得到一个与原始数组不同的数组,print_r 输出永远不会被读回. (例如,当=> Array( 出现在值本身中时。也许很少见,但需要考虑。

标签: php arrays multidimensional-array output


【解决方案1】:
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;
    }
} 

不是我的代码,在 cmets 中找到:print_r “马特”是所有者

【讨论】:

【解决方案2】:

我还为您的问题制定了解决方案,因为它是在 Stack Overflow 上重新创建问题的非常有用的工具。我的来源在这里:https://github.com/etalon/aprp

我做的有点不同:在你的字符串中你不需要换行符,所以我遍历字符。一个缺点:如果您的数组值中有方括号或圆括号,它将不起作用。

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2013-02-08
    • 2021-03-09
    • 2011-10-24
    相关资源
    最近更新 更多