Heim  >  Artikel  >  Backend-Entwicklung  >  Prinzip des Codes zum Wiederherstellen von PHP print_r in einem Array?

Prinzip des Codes zum Wiederherstellen von PHP print_r in einem Array?

WBOY
WBOYOriginal
2016-12-01 00:56:381362Durchsuche

<code>class Trie {  
  protected $dict = array();  
  protected $buf = '';  
  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;  
  }  
  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']) ) {  
        $ar = explode(',', $p['val']);  
        call_user_func_array( array($this, array_shift($ar)), $ar );  
        return $i;  
    }  
    return $t;  
  }  
  function __call($method, $param) {  
    echo "****\n$this->buf 未定义方法:$method 参数:" . join(',', $param) . "<br />\n";  
  }  
}  
  
  
  
class App extends Trie {  
  public $res = array();  
  protected $stack = array();  
  protected $keyname = '';  
  protected $buf = '';  
  function __construct() {  
    $this->stack[] =& $this->res;  
  }  
  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;  
    }  
    $this->buf = '';  
  }  
}</code>

Ich habe diesen Code in mehreren Beispielen gesehen. Ich kenne die Originalquelle nicht. Wie implementiert er print_r, um als Array angezeigt zu werden? Bitte kommentieren, danke!
Die am häufigsten verwendete in der Mitte ist &. Mir wurde nach einigem Hin und Her schwindelig.

Antwortinhalt:

<code>class Trie {  
  protected $dict = array();  
  protected $buf = '';  
  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;  
  }  
  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']) ) {  
        $ar = explode(',', $p['val']);  
        call_user_func_array( array($this, array_shift($ar)), $ar );  
        return $i;  
    }  
    return $t;  
  }  
  function __call($method, $param) {  
    echo "****\n$this->buf 未定义方法:$method 参数:" . join(',', $param) . "<br />\n";  
  }  
}  
  
  
  
class App extends Trie {  
  public $res = array();  
  protected $stack = array();  
  protected $keyname = '';  
  protected $buf = '';  
  function __construct() {  
    $this->stack[] =& $this->res;  
  }  
  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;  
    }  
    $this->buf = '';  
  }  
}</code>

Ich habe diesen Code in mehreren Beispielen gesehen. Ich kenne die Originalquelle nicht. Wie implementiert er print_r, um als Array angezeigt zu werden? Bitte kommentieren, danke!
Die am häufigsten verwendete in der Mitte ist &. Mir wurde nach einigem Hin und Her schwindelig.

& stellt ein Zitat dar, z. B.

<code class="php">$a = &$b;</code>

bedeutet, dass $a und $b dieselbe Referenz sind. Durch Ändern von $a wird auch der Wert von $b geändert, und durch Ändern von $b wird auch $a beeinflusst. Wenn Sie & nicht hinzufügen, bedeutet dies, dass der Wert von $b in $a kopiert wird. Beide Werte sind unabhängig.

Das Hinzufügen von & zur Parameterliste einer Funktion (Methode) bedeutet, dass es als Referenz übergeben wird. Wenn es nicht hinzugefügt wird, bedeutet dies, dass es als Wert übergeben wird sagen.

Es ist zu beachten, dass der Objekttyp eine Referenz ist, unabhängig davon, ob & hinzugefügt wird oder nicht.

Sehen Sie sich das an

Stellungnahme:
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