栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从print_r输出重新创建原始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], 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 ‘是所有者



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/426339.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号