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

黄舟
发布: 2017-02-17 09:57:52
原创
1650人浏览过


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

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

即构数智人
即构数智人

即构数智人是由即构科技推出的AI虚拟数字人视频创作平台,支持数字人形象定制、短视频创作、数字人直播等。

即构数智人 36
查看详情 即构数智人

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 = '';    
    protected $keyname = '';    
    protected $stack = array();    
    public function __construct() {
        $this->stack[] =& $this->res;
    }    public function __call($method, $param){
        echo $this->buf .' not defined mehtod:'.$method. ' param:'.implode(',', $param);
    }    public function set($word, $value=''){
        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['val'] = $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 = '';
            }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['val'])){            
        $arr = explode(',', $p['val']);
            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 = '';
    }    protected function brackets($c){
        $cnt = count($this->stack)-1;        
        switch($c){            
        case ')':                
        if($this->keyname){                    
        $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                
                $this->keyname = '';
                array_pop($this->stack);                
                break;            
                case '[':                
                if($this->keyname){                    
                $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                break;            case ']':                
                $this->keyname = $this->buf;                break;
        }        $this->buf = '';
    }

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



demo.php

<?phprequire 'RestorePrint.class.php';$print_r_data = <<<TXT
Array
(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)
TXT;
// 显示打印的数据echo '显示打印的数据<br>';
echo '<pre>'.$print_r_data.'</pre>';
$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 '还原为数组<br>';
var_dump($result);?>
登录后复制



输出:

立即学习PHP免费学习笔记(深入)”;

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



源码下载地址:点击查看

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 = '';    
    protected $keyname = '';    
    protected $stack = array();    
    public function __construct() {
        $this->stack[] =& $this->res;
    }    public function __call($method, $param){
        echo $this->buf .' not defined mehtod:'.$method. ' param:'.implode(',', $param);
    }    public function set($word, $value=''){
        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['val'] = $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 = '';
            }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['val'])){            
        $arr = explode(',', $p['val']);
            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 = '';
    }    protected function brackets($c){
        $cnt = count($this->stack)-1;        
        switch($c){            
        case ')':                
        if($this->keyname){                    
        $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                
                $this->keyname = '';
                array_pop($this->stack);                
                break;            
                case '[':                
                if($this->keyname){                    
                $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                
                break;            
                case ']':                
                $this->keyname = $this->buf;                
                break;
        }        $this->buf = '';
    }

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



demo.php

<?phprequire 'RestorePrint.class.php';$print_r_data = <<<TXT
Array
(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)
TXT;
// 显示打印的数据echo '显示打印的数据<br>';
echo '<pre>'.$print_r_data.'</pre>';
$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 '还原为数组<br>';
var_dump($result);?>
登录后复制



输出:

立即学习PHP免费学习笔记(深入)”;

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



 以上就是的内容,更多相关内容请关注PHP中文网(www.php.cn)!


相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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