时间:2021-05-02
PHP print_r方法可以把变量打印显示,使变量易于理解。如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素。object与数组类似。print_r用于打印数组较多。
php原生没有把print_r方法打印后的数据还原为原始数组,因此写了下面这个方法,实现将print_r处理后的数据还原为原始数组。
RestorePrint.class.php
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 <?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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <?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)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言说到PHP代码调试,对于有经验的PHPer,通过echo、print_r、var_dump函数,或PHP开发工具zendstudio、editplus可解决
复制代码代码如下:$a=array('a','b','c','d');unset($a[2]);print_r($a);但是这种方法的最大缺点是没有重建数组索引
1、使用print_r($array/$var)print是打印的意思,而r则取自Array的单词,那么该函数的功能就是打印数组内容,它既可以打印数组内容,也可
php中有几种输出形式在php中有5种输出形式,分别是echo、print_r、print、var_dump和die。echo只能输出字符串等单一数据不能输出数
字符串:$str="abcdefg";方法一(直接使用php自带函数strrev($str))print_r(strrev($str));使用for循环方式,s