PHP语言 将print_r处理后的数据还原为原始数组的方法
小标 2018-07-12 来源 : 阅读 1075 评论 0

摘要:本文主要向大家介绍了PHP语言 将print_r处理后的数据还原为原始数组的方法,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言 将print_r处理后的数据还原为原始数组的方法,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

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);

?>

输出:

显示打印的数据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频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程