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

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

内容导读

收集整理的这篇技术教程文章主要介绍了如何通过php将print_r处理后的数据还原为原始数组的方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3952字,纯文字阅读大概需要6分钟

内容图文

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 = <<<TXTArray(

[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 将print_r处理后的数据还原为原始数组的方法,更多相关内容请关注Gxl网。

相关推荐:

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

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

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

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

内容总结

以上是为您收集整理的如何通过php将print_r处理后的数据还原为原始数组的方法全部内容,希望文章能够帮你解决如何通过php将print_r处理后的数据还原为原始数组的方法所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com