Home Backend Development PHP Tutorial How to restore the data processed by print_r to the original array through php

How to restore the data processed by print_r to the original array through php

Jun 20, 2018 pm 05:55 PM
php

php The print_r method can print and display variables to make them easy to understand. If the variable is a string, integer or float, the variable value itself will be printed. If the variable is an array, the keys and elements will be displayed in a certain format. object is similar to an array. print_r is used to print large arrays.

php natively does not restore the data printed by the print_r method to the original array, so the following method was written to restore the data processed by print_r to the original array.

RestorePrint.class.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?>
Copy after login

demo.php

<?phprequire '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;// 显示打印的数据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);?>
Copy after login

Output:

显示打印的数据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)
Copy after login

This article explains how PHP restores the data processed by print_r to the original array. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

How to determine whether the connection is available through PDO in php

Determine the page or picture through php Whether it is compressed by gzip

HTML5 Example of obtaining the current geographical location and displaying it on Baidu Map

The above is the detailed content of How to restore the data processed by print_r to the original array through php. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles