您好,登錄后才能下訂單哦!
這篇文章主要介紹了php將print_r處理后的數據還原為原始數組怎么辦,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
PHP print_r方法可以把變量打印顯示,使變量易于理解。如果變量是string,integer或float,將打印變量值本身,如果變量是array,將會按照一定格式顯示鍵和元素。object與數組類似。print_r用于打印數組較多。
php原生沒有把print_r方法打印后的數據還原為原始數組,因此寫了下面這個方法,實現將print_r處理后的數據還原為原始數組。
RestorePrint.class.php
<?php /** * 將print_r處理后的數據還原為原始數組 * 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
<?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)
感謝你能夠認真閱讀完這篇文章,希望小編分享的“php將print_r處理后的數據還原為原始數組怎么辦”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。