php将print_r处理后的数据还原为原始数组的解决方
PHP print_r方法可以把变量打印显示,使变量易于理解。如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素。object与数组类似。print_r用于打印数组较多。
php原生没有把print_r方法打印后的数据还原为原始数组,写了狼蚁网站SEO优化这个方法,实现将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 ; } $t = count($this->stack)-1; $this->stack[$t][$this->keyname] = array(); $this->stack[] =& $this->stack[$t][$this->keyname]; $this->keyname = ''; } protected function brackets($c){ $t = count($this->stack)-1; switch($c){ case ')': if($this->keyname){ $this->stack[$t][$this->keyname] = trim($this->buf); } $this->keyname = ''; array_pop($this->stack); break; case '[': if($this->keyname){ $this->stack[$t][$this->keyname] = trim($this->buf); } break; case ']': $this->keyname = $this->buf; break; } $this->buf = ''; } } // class end ?>
demo.php
<?php require '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)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程