PHP-Methode zum Wiederherstellen der von print_r verarbeiteten Daten im ursprünglichen Array

黄舟
Freigeben: 2023-03-05 19:18:02
Original
1370 Leute haben es durchsucht


php print_r-Methode kann Variablen drucken und anzeigen, um sie leichter verständlich zu machen. Wenn die Variable ein String, eine Ganzzahl oder eine Gleitkommazahl ist, wird der Variablenwert selbst ausgegeben. Wenn die Variable ein Array ist, werden die Schlüssel und Elemente in einem bestimmten Format angezeigt. Das Objekt ähnelt einem Array. print_r wird zum Drucken großer Arrays verwendet.

PHP stellt die von der print_r-Methode gedruckten Daten nicht nativ im ursprünglichen Array wieder her, daher wurde die folgende Methode geschrieben, um die von print_r verarbeiteten Daten im ursprünglichen Array wiederherzustellen.

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?>
Nach dem Login kopieren



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);?>
Nach dem Login kopieren



Ausgabe:

显示打印的数据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)
Nach dem Login kopieren



Download-Adresse des Quellcodes: Zum Anzeigen klicken

Die PHP-Methode print_r kann Variablen drucken und anzeigen, sodass die Variablen leicht verständlich sind. Wenn die Variable ein String, eine Ganzzahl oder eine Gleitkommazahl ist, wird der Variablenwert selbst ausgegeben. Wenn die Variable ein Array ist, werden die Schlüssel und Elemente in einem bestimmten Format angezeigt. Das Objekt ähnelt einem Array. print_r wird zum Drucken großer Arrays verwendet.

PHP stellt die von der print_r-Methode gedruckten Daten nativ nicht im ursprünglichen Array wieder her, daher wurde die folgende Methode geschrieben, um die von print_r verarbeiteten Daten im ursprünglichen Array wiederherzustellen.

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?>
Nach dem Login kopieren



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);?>
Nach dem Login kopieren



Ausgabe:

显示打印的数据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)
Nach dem Login kopieren



Das Obige ist der Inhalt. Weitere verwandte Inhalte finden Sie auf der chinesischen PHP-Website (www.php.cn).


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!