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

解决方案 »

  1.   

    这么做的目的是什么? 直接从$arr复制一份就好了啊。
      

  2.   

    用json_encode, json_decode 吧。。
      

  3.   

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

  4.   


    解析也不算难, php代码是的  arry( 'key'=> 'v') 这样的格式, 把不一致的地方替换掉, 再eval一下就出来了
      

  5.   

    先贴代码,有关算法问题我将另开贴讨论Trie 键树类,基类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 = '';
      }
    }
    测试代码$arr=array('abcd'=>"sdfasdf",'bbb'=>'lxg','ccc'=>'bbbbbbbbb');
    $ar = array('a'=>$arr, 'b'=>$arr);
    $s = print_r($ar,true);$p = new App;
    $p->set('Array', 'group')
    ->set(' [', 'brackets,[')
    ->set('] => ', 'brackets,]')
    ->set(')', 'brackets,)');
    $p->parse($s);
    print_r($p->res);
    处理结果Array
    (
        [a] => Array
            (
                [abcd] => sdfasdf
                [bbb] => lxg
                [ccc] => bbbbbbbbb
            )    [b] => Array
            (
                [abcd] => sdfasdf
                [bbb] => lxg
                [ccc] => bbbbbbbbb
            ))