从print_r输出重新创建原始PHP数组

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了从print_r输出重新创建原始PHP数组脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有一些来输出,我无法访问原始的 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插件,为我这样做已经在那里?

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],$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“马特”是业主

脚本宝典总结

以上是脚本宝典为你收集整理的从print_r输出重新创建原始PHP数组全部内容,希望文章能够帮你解决从print_r输出重新创建原始PHP数组所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。