登录  /  注册

如何通过php 将print_r处理后的数据还原为原始数组的方法

jacklove
发布: 2018-06-20 17:55:54
原创
2036人浏览过

php print_r方法可以把变量打印显示,使变量易于理解。如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素。object与数组类似。print_r用于打印数组较多。

php原生没有把print_r方法打印后的数据还原为原始数组,因此写了下面这个方法,实现将print_r处理后的数据还原为原始数组。

RestorePrint.class.php

<?php/**
 * 将print_r处理后的数据还原为原始数组
 * Date:    2016-10-31
 * Author:  fdipzone
 * Ver:     1.0
 */class RestorePrint{ // class start

    public $res = array();    protected $dict = array();    protected $buf = &#39;&#39;;    protected $keyname = &#39;&#39;;    protected $stack = array();    public function __construct() {
        $this->stack[] =& $this->res;
    }    public function __call($method, $param){
        echo $this->buf .&#39; not defined mehtod:&#39;.$method. &#39; param:&#39;.implode(&#39;,&#39;, $param);
    }    public function set($word, $value=&#39;&#39;){
        if(is_array($word)){            foreach($word as $k=>$v){                $this->set($k, $v);
            }
        }        $p =& $this->dict;        foreach(str_split($word) as $ch){            if(!isset($p[$ch])){                $p[$ch] = array();
            }            $p =& $p[$ch];
        }        $p[&#39;val&#39;] = $value;        return $this;
    }    public function parse($str){
        $this->doc = $str;        $this->len = strlen($str);        $i = 0;        while($i < $this->len){            $t = $this->find($this->dict, $i);            if($t){                $i = $t;                $this->buf = &#39;&#39;;
            }else{                $this->buf .= $this->doc{$i++};
            }
        }
    }    protected function find(&$p, $i){
        if($i >= $this->len){            return $i;
        }        $t = 0;        $n = $this->doc{$i};        if(isset($p[$n])){            $t = $this->find($p[$n], $i+1);
        }        if($t){            return $t;
        }        if(isset($p[&#39;val&#39;])){            $arr = explode(&#39;,&#39;, $p[&#39;val&#39;]);
            call_user_func_array(array($this, array_shift($arr)), $arr);            return $i;
        }        return $t;
    }    protected function group(){
        if(!$this->keyname){            return ;
        }        $cnt = count($this->stack)-1;        $this->stack[$cnt][$this->keyname] = array();        $this->stack[] =& $this->stack[$cnt][$this->keyname];        $this->keyname = &#39;&#39;;
    }    protected function brackets($c){
        $cnt = count($this->stack)-1;        switch($c){            case &#39;)&#39;:                if($this->keyname){                    $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                $this->keyname = &#39;&#39;;
                array_pop($this->stack);                break;            case &#39;[&#39;:                if($this->keyname){                    $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                break;            case &#39;]&#39;:                $this->keyname = $this->buf;                break;
        }        $this->buf = &#39;&#39;;
    }

} // class end?>
登录后复制

demo.php

<?phprequire &#39;RestorePrint.class.php&#39;;$print_r_data = <<<TXT
Array
(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)
TXT;// 显示打印的数据echo &#39;显示打印的数据<br>&#39;;echo &#39;<pre class="brush:php;toolbar:false">&#39;.$print_r_data.&#39;
登录后复制
';$oRestorePrint = new RestorePrint;$oRestorePrint->set('Array', 'group');$oRestorePrint->set(' [', 'brackets,[');$oRestorePrint->set('] => ', 'brackets,]');$oRestorePrint->set(')', 'brackets,)');$oRestorePrint->parse($print_r_data);$result = $oRestorePrint->res;echo '还原为数组
'; var_dump($result);?>

输出:

显示打印的数据Array(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)
还原为数组array (size=5)  &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  &#39;gender&#39; => string &#39;male&#39; (length=4)  &#39;age&#39; => string &#39;18&#39; (length=2)  &#39;profession&#39; => string &#39;programmer&#39; (length=10)  &#39;detail&#39; => 
    array (size=2)      &#39;grade&#39; => string &#39;1&#39; (length=1)      &#39;addtime&#39; => string &#39;2016-10-31&#39; (length=10)
登录后复制

本文讲解了php 将print_r处理后的数据还原为原始数组的方法,更多相关内容请关注php中文网。

相关推荐:

通过php中的PDO判断连接是否可用的方法

通过php 判断页面或图片是否经过gzip压缩

HTML5获取当前地理位置并在百度地图上展示的实例

以上就是如何通过php 将print_r处理后的数据还原为原始数组的方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号