首頁  >  文章  >  後端開發  >  如何透過php 將print_r處理後的資料還原為原始數組的方法

如何透過php 將print_r處理後的資料還原為原始數組的方法

jacklove
jacklove原創
2018-06-20 17:55:542203瀏覽

php print_r方法可以把變數列印顯示,使變數易於理解。如果變數是string,integer或float,將列印變數值本身,如果變數是array,將會依照一定格式顯示鍵和元素。 object與數組類似。 print_r用於列印數組較多。

php原生沒有把print_r方法列印後的資料還原為原始數組,因此寫了下面這個方法,實作將print_r處理後的資料還原為原始數組。

RestorePrint.class.php

#
<?php/**
 * 将print_r处理后的数据还原为原始数组
 * Date:    2016-10-31
 * Author:  fdipzone
 * Ver:     1.0
 */class RestorePrint{ // class start

    public $res = array();    protected $dict = array();    protected $buf = &#39;&#39;;    protected $keyname = &#39;&#39;;    protected $stack = array();    public function __construct() {
        $this->stack[] =& $this->res;
    }    public function __call($method, $param){
        echo $this->buf .&#39; not defined mehtod:&#39;.$method. &#39; param:&#39;.implode(&#39;,&#39;, $param);
    }    public function set($word, $value=&#39;&#39;){
        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[&#39;val&#39;] = $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 = &#39;&#39;;
            }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[&#39;val&#39;])){            $arr = explode(&#39;,&#39;, $p[&#39;val&#39;]);
            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 = &#39;&#39;;
    }    protected function brackets($c){
        $cnt = count($this->stack)-1;        switch($c){            case &#39;)&#39;:                if($this->keyname){                    $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                $this->keyname = &#39;&#39;;
                array_pop($this->stack);                break;            case &#39;[&#39;:                if($this->keyname){                    $this->stack[$cnt][$this->keyname] = trim($this->buf);
                }                break;            case &#39;]&#39;:                $this->keyname = $this->buf;                break;
        }        $this->buf = &#39;&#39;;
    }

} // class end?>

demo.php




<?phprequire &#39;RestorePrint.class.php&#39;;$print_r_data = <<<TXT
Array
(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)
TXT;// 显示打印的数据echo &#39;显示打印的数据<br>&#39;;echo &#39;<pre class="brush:php;toolbar:false">&#39;.$print_r_data.&#39;
';$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 '还原为数组
'; var_dump($result);?>######輸出:######
显示打印的数据Array(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)
还原为数组array (size=5)  &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  &#39;gender&#39; => string &#39;male&#39; (length=4)  &#39;age&#39; => string &#39;18&#39; (length=2)  &#39;profession&#39; => string &#39;programmer&#39; (length=10)  &#39;detail&#39; => 
    array (size=2)      &#39;grade&#39; => string &#39;1&#39; (length=1)      &#39;addtime&#39; => string &#39;2016-10-31&#39; (length=10)
###本文說明了php 將print_r處理後的資料還原為原始陣列的方法,更多相關內容請關注php中文網。 ######相關推薦:#########透過php中的PDO判斷連接是否可用的方法###############透過php 判斷頁面或圖片是否經過gzip壓縮###############HTML5取得目前地理位置並在百度地圖上展示的實例#########

以上是如何透過php 將print_r處理後的資料還原為原始數組的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn