php將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的解決方法
PHP print_r方法可以把變量打印顯示,使變量易于理解。如果變量是string,integer或float,將打印變量值本身,如果變量是array,將會(huì)按照一定格式顯示鍵和元素。object與數(shù)組類似。print_r用于打印數(shù)組較多。
php原生沒(méi)有把print_r方法打印后的數(shù)據(jù)還原為原始數(shù)組,因此寫了下面這個(gè)方法,實(shí)現(xiàn)將print_r處理后的數(shù)據(jù)還原為原始數(shù)組。
RestorePrint.class.php
<?php
/**
* 將print_r處理后的數(shù)據(jù)還原為原始數(shù)組
* 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
<?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;
// 顯示打印的數(shù)據(jù)
echo '顯示打印的數(shù)據(jù)<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 '還原為數(shù)組<br>';
var_dump($result);
?>
輸出:
顯示打印的數(shù)據(jù)
Array
(
[name] => fdipzone
[gender] => male
[age] => 18
[profession] => programmer
[detail] => Array(
[grade] => 1
[addtime] => 2016-10-31
)
)
還原為數(shù)組
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)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信公眾平臺(tái)開(kāi)發(fā)教程②微信端分享功能圖文詳解
這篇文章主要介紹了微信公眾平臺(tái)開(kāi)發(fā)微信端分享功能,結(jié)合圖文形式詳細(xì)分析了微信分享功能的原理、操作步驟及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04
PHP錯(cuò)誤Parse error: syntax error, unexpected end of file in te
這篇文章主要介紹了PHP錯(cuò)誤Parse error: syntax error, unexpected end of file in test.php on line 12解決方法,需要的朋友可以參考下2014-06-06
php實(shí)現(xiàn)與python進(jìn)行socket通信的方法示例
這篇文章主要介紹了php實(shí)現(xiàn)與python進(jìn)行socket通信的方法,結(jié)合實(shí)例形式分析了php使用自定義類發(fā)送socket請(qǐng)求數(shù)據(jù)及Python接收socket數(shù)據(jù)并處理請(qǐng)求等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
PHP基于socket實(shí)現(xiàn)客戶端和服務(wù)端通訊功能
這篇文章主要介紹了PHP基于socket實(shí)現(xiàn)客戶端和服務(wù)端通訊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

