找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1889|回复: 0
打印 上一主题 下一主题

php将print_r处理后的数据还原为原始数组的解决方法

[复制链接]

2500

主题

2513

帖子

7520

积分

论坛元老

Rank: 8Rank: 8

积分
7520
跳转到指定楼层
楼主
发表于 2018-2-14 05:31:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            PHP print_r方法可以把变量打印显示,使变量易于理解。如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素。object与数组类似。print_r用于打印数组较多。
php原生没有把print_r方法打印后的数据还原为原始数组,因此写了下面这个方法,实现将print_r处理后的数据还原为原始数组。
RestorePrint.class.php
stack[] =& $this->res;
  }
  public function __call($method, $param){
    echo $this->buf .' not defined mehtod:'.$method. ' param:'.implode(',', $param);
  }
  public 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;
  }
  public 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'])){
      $arr = explode(',', $p['val']);
      call_user_func_array(array($this, array_shift($arr)), $arr);
      return $i;
    }
    return $t;
  }
  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;
        break;
    }
    $this->buf = '';
  }
} // class end
?>
demo.php
fdipzone
  [gender] => male
  [age] => 18
  [profession] => programmer
  [detail] => Array(
    [grade] => 1
    [addtime] => 2016-10-31
  )
)
TXT;
// 显示打印的数据
echo '显示打印的数据
';
echo ''.$print_r_data.'';
$oRestorePrint = new RestorePrint;
$oRestorePrint->set('Array', 'group');
$oRestorePrint->set(' [', 'brackets,[');
$oRestorePrint->set('] => ', 'brackets,]');
$oRestorePrint->set(')', 'brackets,)');
$oRestorePrint->parse($print_r_data);
$result = $oRestorePrint->res;
echo '还原为数组
';
var_dump($result);
?>
输出:
显示打印的数据
Array
(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)
还原为数组
array (size=5)
'name' => string 'fdipzone' (length=8)
'gender' => string 'male' (length=4)
'age' => string '18' (length=2)
'profession' => string 'programmer' (length=10)
'detail' =>
array (size=2)
'grade' => string '1' (length=1)
'addtime' => string '2016-10-31' (length=10)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
            
            
您可能感兴趣的文章:
  • 使用lua实现php的print_r()函数功能
  • php中print(),print_r(),echo()的区别详解
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端