php將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的解決方法
PHP print_r方法可以把變量打印顯示,使變量易于理解。如果變量是string,integer或float,將打印變量值本身,如果變量是array,將會(huì)按照一定格式顯示鍵和元素。object與數(shù)組類(lèi)似。print_r用于打印數(shù)組較多。
php原生沒(méi)有把print_r方法打印后的數(shù)據(jù)還原為原始數(shù)組,因此寫(xiě)了下面這個(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)文章
PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法
下面小編就為大家分享一篇PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
php 讀取shell管道傳輸過(guò)來(lái)的內(nèi)容
已經(jīng)寫(xiě)了不少后臺(tái)運(yùn)行的deamon了.用的挺順手的,但是我現(xiàn)在想獲取管道傳過(guò)來(lái)的內(nèi)容,不知道咋實(shí)現(xiàn),類(lèi)似echo "aaaa" |a.php,a.php怎么獲得echo的內(nèi)容,不知道您有什么高見(jiàn).2010-03-03
PHP實(shí)現(xiàn)守護(hù)進(jìn)程的示例代碼
守護(hù)進(jìn)程到底是怎么實(shí)現(xiàn)的?為什么有的程序既可以自己就成為守護(hù)進(jìn)程,又可以通過(guò)systemd 來(lái)后臺(tái)運(yùn)行?本文將為大家具體講解,感興趣的可以了解一下2022-05-05
PHP函數(shù)按引用傳遞參數(shù)及函數(shù)可選參數(shù)用法示例
這篇文章主要介紹了PHP函數(shù)按引用傳遞參數(shù)及函數(shù)可選參數(shù)用法,結(jié)合實(shí)例形式分析了php函數(shù)的引用傳參與可選參數(shù)具體使用技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06
通過(guò)PHP實(shí)現(xiàn)獲取訪(fǎng)問(wèn)用戶(hù)IP
這篇文章主要介紹了通過(guò)PHP實(shí)現(xiàn)獲取訪(fǎng)問(wèn)用戶(hù)IP,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
session在php5.3中的變化 session_is_registered() is deprecated in
在php 5.3中session_is_registered()已經(jīng)是放棄使用了,大家在使用過(guò)程中需要注意一下了2013-11-11
php獲取遠(yuǎn)程文件內(nèi)容的函數(shù)
這篇文章主要介紹了關(guān)于php獲取遠(yuǎn)程文件內(nèi)容的函數(shù)代碼,使用這個(gè)函數(shù)也可以獲取圖片2015-11-11
PHP讀取并輸出XML文件數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了PHP讀取并輸出XML文件數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法,涉及php針對(duì)xml格式文件數(shù)據(jù)的載入、遍歷、讀取、輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12

