php 将print_r处理后的数据还原为原始数组的方法
来源:互联网 发布:g2553烧录软件 编辑:程序博客网 时间:2024/06/10 12:18
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)
源码下载地址:点击查看
2 0
- php 将print_r处理后的数据还原为原始数组的方法
- php将数组转化为json数据遇到的问题解决
- php如何将数组保存为文件的方法
- javascript仿php的print_r函数输出json数据
- javascript仿php的print_r函数输出json数据
- php怎么将数组数组转化为json格式的数据
- 面程序的功能是将一个4×4的数组进行逆时针旋转90度后输出,要求原始数组的数据随机输入,原新数组以4行4列的方式输出
- C# Datagridview绑定数据后将一列设置为DataGridViewLinkColumn的方法
- js 处理 php 的数组数据
- php字符串解析为数组的方法
- ajax中调试数据,在php处理中,用print_r;
- JS解析json数据并将json字符串转化为数组的实现方法
- 将浮点型数据转化为人民币读法字符串的方法(数组实现)
- JS解析json数据并将json字符串转化为数组的实现方法
- 将由BeanUtils的getProperty方法返回的Date对象的字符串表示还原为对象
- php中echo(),print(),print_r()的区别
- echo(),print(),print_r()的区别 -php
- php中echo(),print(),print_r()的区别
- 2016hua为校招:笔试上机题目:游程编码、解码
- socket通讯中既可以发送文本又可以发送图片
- JQuery 现实 三级联动菜单
- SVM和SVR
- CODEVS 1039数的划分
- php 将print_r处理后的数据还原为原始数组的方法
- 编程珠玑
- scala中内部函数的使用
- python 虚拟环境virtualenv的安装和使用
- 小菜谈谈之缓冲区溢出
- LeetCode 56 Merge Intervals
- 面试之javaSe(二)
- 菜鸟重拾C语言之路(1)
- 【openjudge 1759】最长上升子序列(dp)