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-> ......