Rumah > pembangunan bahagian belakang > tutorial php > php print_r 还原成数组的代码原理?

php print_r 还原成数组的代码原理?

WBOY
Lepaskan: 2023-03-01 22:42:01
asal
1411 orang telah melayarinya

<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 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>
Salin selepas log masuk
Salin selepas log masuk

这段代码我在几个示例中看到,原始的出处不知道,它是如何实现print_r 的显示成数组呢?求注释,谢谢!
中间常用的是&,来回几个我就晕了。

回复内容:

<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 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>
Salin selepas log masuk
Salin selepas log masuk

这段代码我在几个示例中看到,原始的出处不知道,它是如何实现print_r 的显示成数组呢?求注释,谢谢!
中间常用的是&,来回几个我就晕了。

&代表引用,比如

<code class="php">$a = &$b;</code>
Salin selepas log masuk

就代表$a$b是同一个引用,修改$a也会修改$b的值,修改$b也影响$a。不加上&就代表把$b的值复制一份给$a,两者都是独立的。

而函数(方法)的参数列表里加上&,就代表按引用传递了,不加就是按值传递,这是编程的基本概念,不用多说了吧。

需要注意的是对象类型,不管加不加&都是引用。

看看这个

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan