How to restore an array of strings generated by print_r ($ arr, true)-php Tutorial

Source: Internet
Author: User
How to restore the string generated by print_r ($ arr, true) to an array. for example, I have a $ arrarray (abcd & gt; sdfasdf, bbb & gt; lxg, ccc & gt; bbbbbbbbb); $ strprint_r ($ rr, true); I want to change $ str to $ arr, which is an array variable. Does anyone know? ------ Solution ---- how to restore an array of strings generated by print_r ($ arr, true)
For example, I have
$ Arr = array ('ABCD' => 'sdfasdf ', 'BBB' => 'lxg', 'CCC '=> 'bbbbbbbbbb ');
$ Str = print_r ($ rr, true );
I want to change $ str to $ arr, which is an array variable. Does anyone know?



------ Solution --------------------
What is the purpose of doing so? Just copy one copy from $ arr.
------ Solution --------------------
Use json_encode and json_decode ..
------ Solution --------------------
One-dimensional arrays can also be processed. for multi-dimensional arrays, it is very challenging to process them...
------ Solution --------------------
Discussion

The method for storing data in the connected project is $ str = print_r ($ rr, true); the data already exists in the database. It can only be parsed now.

------ Solution --------------------
Post the code first. I will discuss the algorithm issues.

Trie key Tree class, base class
PHP code
Class Trie {protected $ dict = array (); protected $ buf = ''; 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;} 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 ']) {$ ar = explode (', ', $ p ['Val']); call_user_func_array (array ($ this, array_shift ($ ar), $ ar); return $ I;} return $ t;} function _ call ($ method, $ param) {echo "***** \ n $ this-> buf undefined method: $ method parameter :". join (',', $ param )."
\ N ";}}
------ Solution --------------------
Discussion

Post the code first. I will discuss the algorithm issues.

Trie key Tree class, base class PHP code
Class Trie {
Protected $ dict = array ();
Protected $ buf = '';
Function set ($ word, $ value = ''){
If (is_array ($ word) foreach ($ word as $ k =>v v) $ this-> ......

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.