PHP建站技术分享-从入门到精通_各类知识收集PHP建站技术分享-从入门到精通_各类知识收集PHP建站技术分享-从入门到精通_各类知识收集

QQ:420220301 微信/手机:150-3210-7690
当前位置:首页 > 工作总结

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

管理员 2025-02-19
工作总结
15

《PHP编程:php将print_r处理后的数据还原为原始数组的解决方法》要点:
本文介绍了PHP编程:php将print_r处理后的数据还原为原始数组的解决方法,希望对您有用。如果有疑问,可以联系我们。
 

PHP实例PHP print_r方法可以把变量打印显示,使变量易于理解.如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素.object与数组类似.print_r用于打印数组较多.

PHP实例php原生没有把print_r方法打印后的数据还原为原始数组,因此写了下面这个方法,实现将print_r处理后的数据还原为原始数组.

PHP实例RestorePrint.class.php

PHP实例<?php/** * 将print_r处理后的数据还原为原始数组 * Date:  2016-10-31 * Author: fdipzone * Ver:   1.0 */class RestorePrint{ // class start  public $res = array();  protected $dict = array();  protected $buf = '';  protected $keyname = '';  protected $stack = array();  public function __construct() {    $this->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 < $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'])){      $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?>

PHP实例demo.php

PHP实例<?phprequire 'RestorePrint.class.php';$print_r_data = <<<TXTArray(  [name] => fdipzone  [gender] => male  [age] => 18  [profession] => programmer  [detail] => Array(    [grade] => 1    [addtime] => 2016-10-31  ))TXT;// 显示打印的数据echo '显示打印的数据<br>';echo '<pre>'.$print_r_data.'</pre>';$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 '还原为数组<br>';var_dump($result);?>

PHP实例输出:

PHP实例显示打印的数据

PHP实例Array
(
    [name] => fdipzone
    [gender] => male
    [age] => 18
    [profession] => programmer
    [detail] => Array(
        [grade] => 1
        [addtime] => 2016-10-31
    )
)

PHP实例还原为数组

PHP实例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)

PHP实例以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持维易PHP.

转载请注明本页网址:
/jiaocheng/2895.html


希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, LeCMS, ClassCMS, Fastadmin, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。

相关推荐

扫码关注

qrcode

QQ交谈

回顶部