最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的解決方法

 更新時(shí)間:2016年11月02日 17:13:11   作者:傲雪星楓  
下面小編就為大家?guī)硪黄猵hp中將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考

PHP print_r方法可以把變量打印顯示,使變量易于理解。如果變量是string,integer或float,將打印變量值本身,如果變量是array,將會(huì)按照一定格式顯示鍵和元素。object與數(shù)組類似。print_r用于打印數(shù)組較多。

php原生沒有把print_r方法打印后的數(shù)據(jù)還原為原始數(shù)組,因此寫了下面這個(gè)方法,實(shí)現(xiàn)將print_r處理后的數(shù)據(jù)還原為原始數(shù)組。

RestorePrint.class.php

<?php
/**
 * 將print_r處理后的數(shù)據(jù)還原為原始數(shù)組
 * 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
?>

demo.php

<?php
require 'RestorePrint.class.php';

$print_r_data = <<<TXT
Array
(
  [name] => fdipzone
  [gender] => male
  [age] => 18
  [profession] => programmer
  [detail] => Array(
    [grade] => 1
    [addtime] => 2016-10-31
  )
)
TXT;

// 顯示打印的數(shù)據(jù)
echo '顯示打印的數(shù)據(jù)<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 '還原為數(shù)組<br>';
var_dump($result);

?>

輸出:

顯示打印的數(shù)據(jù)

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

還原為數(shù)組

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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法

    PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法

    下面小編就為大家分享一篇PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • php 讀取shell管道傳輸過來的內(nèi)容

    php 讀取shell管道傳輸過來的內(nèi)容

    已經(jīng)寫了不少后臺(tái)運(yùn)行的deamon了.用的挺順手的,但是我現(xiàn)在想獲取管道傳過來的內(nèi)容,不知道咋實(shí)現(xiàn),類似echo "aaaa" |a.php,a.php怎么獲得echo的內(nèi)容,不知道您有什么高見.
    2010-03-03
  • PHP實(shí)現(xiàn)守護(hù)進(jìn)程的示例代碼

    PHP實(shí)現(xiàn)守護(hù)進(jìn)程的示例代碼

    守護(hù)進(jìn)程到底是怎么實(shí)現(xiàn)的?為什么有的程序既可以自己就成為守護(hù)進(jìn)程,又可以通過systemd 來后臺(tái)運(yùn)行?本文將為大家具體講解,感興趣的可以了解一下
    2022-05-05
  • PHP多個(gè)文件上傳到服務(wù)器實(shí)例

    PHP多個(gè)文件上傳到服務(wù)器實(shí)例

    這篇文章主要介紹了PHP多個(gè)文件上傳到服務(wù)器的實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)講解了多文件傳遞過程中的數(shù)組應(yīng)用及數(shù)據(jù)遍歷上傳等技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • PHP函數(shù)按引用傳遞參數(shù)及函數(shù)可選參數(shù)用法示例

    PHP函數(shù)按引用傳遞參數(shù)及函數(shù)可選參數(shù)用法示例

    這篇文章主要介紹了PHP函數(shù)按引用傳遞參數(shù)及函數(shù)可選參數(shù)用法,結(jié)合實(shí)例形式分析了php函數(shù)的引用傳參與可選參數(shù)具體使用技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-06-06
  • 通過PHP實(shí)現(xiàn)獲取訪問用戶IP

    通過PHP實(shí)現(xiàn)獲取訪問用戶IP

    這篇文章主要介紹了通過PHP實(shí)現(xiàn)獲取訪問用戶IP,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • PHP下載生成的csv文件及問題總結(jié)

    PHP下載生成的csv文件及問題總結(jié)

    CSV文件最早用在簡(jiǎn)單的數(shù)據(jù)庫里,由于其格式簡(jiǎn)單,并具備很強(qiáng)的開放性,所以起初被掃圖家用作自己圖集的標(biāo)記。CSV文件是個(gè)純文本文件,每一行表示一張圖片的許多屬性。下面小編給大家介紹下PHP下載生成的csv文件及問題總結(jié)
    2015-08-08
  • session在php5.3中的變化 session_is_registered() is deprecated in

    session在php5.3中的變化 session_is_registered() is deprecated in

    在php 5.3中session_is_registered()已經(jīng)是放棄使用了,大家在使用過程中需要注意一下了
    2013-11-11
  • php獲取遠(yuǎn)程文件內(nèi)容的函數(shù)

    php獲取遠(yuǎn)程文件內(nèi)容的函數(shù)

    這篇文章主要介紹了關(guān)于php獲取遠(yuǎn)程文件內(nèi)容的函數(shù)代碼,使用這個(gè)函數(shù)也可以獲取圖片
    2015-11-11
  • PHP讀取并輸出XML文件數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法

    PHP讀取并輸出XML文件數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法

    這篇文章主要介紹了PHP讀取并輸出XML文件數(shù)據(jù)的簡(jiǎn)單實(shí)現(xiàn)方法,涉及php針對(duì)xml格式文件數(shù)據(jù)的載入、遍歷、讀取、輸出等相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12

最新評(píng)論

广丰县| 卫辉市| 石棉县| 曲阳县| 绥棱县| 黄梅县| 兴文县| 天镇县| 汪清县| 沈丘县| 乌审旗| 屯门区| 河北区| 佛教| 长子县| 富川| 石景山区| 诏安县| 莱州市| 文登市| 金山区| 兴隆县| 丹棱县| 禹州市| 辽宁省| 太保市| 阿克苏市| 广水市| 大宁县| 从江县| 临沂市| 广南县| 岳阳市| 上饶县| 通渭县| 成武县| 丰原市| 麻城市| 玛沁县| 安新县| 来安县|