Home > Backend Development > PHP Tutorial > 数组经print_r($arr,true)生成的字符串怎么还原成数组

数组经print_r($arr,true)生成的字符串怎么还原成数组

WBOY
Release: 2016-06-13 13:25:02
Original
1003 people have browsed it

数组经print_r($arr,true)生成的字符串如何还原成数组
比如我现在有一个
$arr=array('abcd'=>'sdfasdf','bbb'=>'lxg','ccc'=>'bbbbbbbbb');
$str=print_r($rr,true);
我要把$str变成$arr 也就是数组变量,有谁知道吗?



------解决方案--------------------
这么做的目的是什么? 直接从$arr复制一份就好了啊。
------解决方案--------------------
用json_encode, json_decode 吧。。
------解决方案--------------------
一维数组还可以处理,如果是多维数组,处理起来很有挑战性啊。。。
------解决方案--------------------

探讨

接的项目,里面的存储数据的方式就是 $str=print_r($rr,true); 存到数据库里的,数据已经存在了。现在只能解析它。

------解决方案--------------------
先贴代码,有关算法问题我将另开贴讨论

Trie 键树类,基类
PHP 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";
  }
}
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

先贴代码,有关算法问题我将另开贴讨论

Trie 键树类,基类PHP code
class Trie {
protected $dict = array();
protected $buf = '';
function set($word, $value='') {
if(is_array($word)) foreach($word as $k=>$v) $this->……
Related labels:
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