php将print_r处理后的数据还原为原始数组的解决方法
PHP print_r方法可以把变量打印显示,使变量易于理解。如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素。object与数组类似。print_r用于打印数组较多。 php原生没有把print_r方法打印后的数据还原为原始数组,因此写了下面这个方法,实现将print_r处理后的数据还原为原始数组。 RestorePrint.class.php public $res = array();
protected $dict = array(); protected $buf = ''; protected $keyname = ''; protected $stack = array(); public function __construct() { public function __call($method,$param){ public function set($word,$value=''){ public function parse($str){ protected function find(&$p,$i){ protected function group(){ protected function brackets($c){
} } // class end demo.php $print_r_data = <<<TXT
Array ( [name] => fdipzone [gender] => male [age] => 18 [profession] => programmer [detail] => Array( [grade] => 1 [addtime] => 2016-10-31 ) ) TXT; // 显示打印的数据 '.$print_r_data.''; $oRestorePrint = new RestorePrint; $oRestorePrint->parse($print_r_data); echo '还原为数组 ?> 输出: 显示打印的数据 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) 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |