Php restores the data processed by print_r to the original array.

Source: Internet
Author: User
The PHPprint_r method can print and display variables to make them easy to understand. If the variable is string, integer, or float, the variable value itself is printed. if the variable is array, the key and element are displayed in a certain format. Objects are similar to arrays. Print_r is used to print a large number of arrays.


The php print_r method can print and display variables to make them easy to understand. If the variable is string, integer, or float, the variable value itself is printed. if the variable is array, the key and element are displayed in a certain format. Objects are similar to arrays. Print_r is used to print a large number of arrays.

Php does not restore the data printed by the print_r method to the original array. Therefore, the following method is written to restore the data processed by print_r to the original array.

RestorePrint. class. php

 Stack [] = & $ this-> res;} public function _ call ($ method, $ param) {echo $ this-> buf. 'Not defined mehtodd :'. $ 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

 Fdipzone [gender] => male [age] => 18 [partition Sion] => programmer [detail] => Array ([grade] => 1 [addtime] => 2016-10-31 )) TXT; // Display printed data echo 'display printed data
'; 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 'is restored to an array
'; Var_dump ($ result);?>



Output:

Display the printed data Array ([name] => fdipzone [gender] => male [age] => 18 [partition Sion] => programmer [detail] => Array ([grade] => 1 [addtime] => 2016-10-31 )) returns to array (size = 5) 'name' => string 'fdipzone '(length = 8) 'gender' => string 'male' (length = 4) & apos; age & apos; = & apos; string & apos; 18 & apos; (length = 2) & apos; comment & apos; = & apos; string & apos; programmer & apos; (length = 10) & apos; detail & apos) 'grade '=> string '1' (length = 1) 'addtime' => string '2017-10-31' (length = 10)



Source code: Click to view

The php print_r method can print and display variables to make them easy to understand. If the variable is string, integer, or float, the variable value itself is printed. if the variable is array, the key and element are displayed in a certain format. Objects are similar to arrays. Print_r is used to print a large number of arrays.

Php does not restore the data printed by the print_r method to the original array. Therefore, the following method is written to restore the data processed by print_r to the original array.

RestorePrint. class. php

 Stack [] = & $ this-> res;} public function _ call ($ method, $ param) {echo $ this-> buf. 'Not defined mehtodd :'. $ 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

 Fdipzone [gender] => male [age] => 18 [partition Sion] => programmer [detail] => Array ([grade] => 1 [addtime] => 2016-10-31 )) TXT; // Display printed data echo 'display printed data
'; 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 'is restored to an array
'; Var_dump ($ result);?>



Output:

Display the printed data Array ([name] => fdipzone [gender] => male [age] => 18 [partition Sion] => programmer [detail] => Array ([grade] => 1 [addtime] => 2016-10-31 )) returns to array (size = 5) 'name' => string 'fdipzone '(length = 8) 'gender' => string 'male' (length = 4) & apos; age & apos; = & apos; string & apos; 18 & apos; (length = 2) & apos; comment & apos; = & apos; string & apos; programmer & apos; (length = 10) & apos; detail & apos) 'grade '=> string '1' (length = 1) 'addtime' => string '2017-10-31' (length = 10)



The above is the content. For more information, please follow the PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.