Principle of the code to restore php print_r to an array?

WBOY
Release: 2023-03-01 22:42:01
Original
1391 people have browsed it

<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>
Copy after login
Copy after login

I have seen this code in several examples. I don’t know the original source. How does it implement print_r to be displayed as an array? Please comment, thank you!
The most commonly used one in the middle is &. I get dizzy after going back and forth a few times.

Reply content:

<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>
Copy after login
Copy after login

I have seen this code in several examples. I don’t know the original source. How does it implement print_r to be displayed as an array? Please comment, thank you!
The most commonly used one in the middle is &. I get dizzy after going back and forth a few times.

& represents a quote, such as

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

means that $a and $b are the same reference. Modifying $a will also modify the value of $b, and modifying $b will also affect $a. Not adding & means copying the value of $b to $a, both of which are independent.

And adding & to the parameter list of a function (method) means that it is passed by reference. If it is not added, it means that it is passed by value. This is a basic concept of programming. Needless to say.

It should be noted that the object type, whether added or not &, is a reference.

Look at this

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template