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

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) . "

\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 = '';

}

}

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

中间常用的是&,来回几个我就晕了。

回复内容:

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) . "

\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 = '';

}

}

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

中间常用的是&,来回几个我就晕了。

&代表引用,比如

$a = &$b;

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

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

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

看看这个

相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值