>  기사  >  백엔드 개발  >  PHP print_r을 배열로 복원하는 코드의 원리는 무엇입니까?

PHP print_r을 배열로 복원하는 코드의 원리는 무엇입니까?

WBOY
WBOY원래의
2016-12-01 00:56:381362검색

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

이 코드는 여러 예에서 보았지만 원본 소스를 알지 못합니다. 배열로 표시되도록 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 < $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>

이 코드는 여러 예에서 보았지만 원본 소스를 알지 못합니다. 배열로 표시되도록 print_r을 어떻게 구현합니까? 댓글을 달아주세요, 감사합니다!
중간에 가장 많이 쓰이는 것은 & 입니다. 몇 번 왔다 갔다 하다가 어지러워졌습니다.

&

과 같은 인용문을 나타냅니다.
<code class="php">$a = &$b;</code>

$a$b이 동일한 참조라는 의미입니다. $a을 수정하면 $b의 값도 수정되고, $b을 수정하면 $a에도 영향을 줍니다. &을 추가하지 않는다는 것은 $b의 값을 $a에 복사한다는 의미이며 둘 다 독립적입니다.

함수(메서드)의 매개변수 목록에 &를 추가한다는 것은 참조로 전달한다는 의미입니다. 추가하지 않으면 값으로 전달한다는 의미입니다. 이는 프로그래밍의 기본 개념입니다. 말하다.

오브젝트 타입은 & 추가 여부와 관계없이 참고용이라는 점에 유의하시기 바랍니다.

이거 보세요

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.