In php, the string is converted to the array functions explode () and implode (). In php, the most common way to convert arrays and strings is to use the explode () and implode () functions for conversion. Today, a friend asked me this question, next I will sort out the most common method for converting arrays and strings in php to each other, that is, using the explode () and implode () functions for conversion, today, I saw a friend ask this question. I will sort it out and share it with you.
See the two functions.
Implode function:
Use the implode function to convert an array to a string.
Explode function:
Use the function explode to convert a string to an array.
Example 1.
Today, I saw a post in the php forum asking my friend how to convert a string into an array. as a new php programmer, the first response is to think of explode (), implode () these two functions. The new one is also converted to an array using the functions in it.
Con [1] = 28 & selt [1] = 1 & con [2] = 29 & selt [2] = 4 & con [3] = 26 & selt [3] = 4
& Con [4] = 30 & selt [4] = 2 & con [5] = 4 & selt [5] = 1 & con [6] = 11 & con [7] = 12
The above is the string that netizens need to convert to the PHP array; below is the new PHP conversion code
The code is as follows: |
|
$ Str = 'con [1] = 28 & selt [1] = 1 & con [2] = 29 & selt [2] = 4 & con [3] = 26 & selt [3] = 4 & con [4] = 30 & selt [4] = 2 & con [5] = 4 & selt [5] = 1 & con [6] = 11 & con [7] = 12 '; $ Arr = explode ('&', $ str ); $ Arr2 = array (); Foreach ($ arr as $ k =>$ v ){ $ Arr = explode ('=', $ v ); $ Arr2 [$ k] = $ arr [1]; } Print_r ($ arr2 ); ?> // Output Array ( [0] => 28 [1] => 1 [2] => 29 [3] => 4 [4] => 26 [5] => 4 [6] => 30 [7] => 2 [8] => 4 [9] => 1 [10] => 11 [11] => 12 ) // Second $ Str = 'con [1] = 28 & selt [1] = 1 & con [2] = 29 & selt [2] = 4 & con [3] = 26 & selt [3] = 4 & con [4] = 30 & selt [4] = 2 & con [5] = 4 & selt [5] = 1 & con [6] = 11 & con [7] = 12 '; $ Arr = explode ('&', $ str ); $ Arr2 = array (); Foreach ($ arr as $ k =>$ v ){ $ Arr = explode ('=', $ v ); $ Arr2 [$ arr [0] = $ arr [1]; } Print_r ($ arr2 ); ?> // Output Array ( [Con [1] => 28 [Selt [1] => 1 [Con [2] => 29 [Selt [2] => 4 [Con [3] => 26 [Selt [3] => 4 [Con [4] => 30 [Selt [4] => 2 [Con [5] => 4 [Selt [5] => 1 [Con [6] => 11 [Con [7] => 12 ) |
Example 2.
The above only supports one-dimensional data. what if it is two-dimensional or three-dimensional data?
The code is as follows: |
|
// Convert all values in a multi-dimensional array to a string ??? Supports up to 3D arrays Function implodex ($ glue, $ array, $ separator = ''){ If (! Is_array ($ array) return $ array; $ String = array ();
$ Count = 0; Foreach ($ array as $ key => $ val ){ If (is_array ($ val )) $ Val = implode ($ glue, $ val ); If ($ count = 0 ){ $ String [] = "{$ val }"; } Else { $ String [] = "{$ glue} {$ val }"; } } If (empty ($ separator) $ separator = $ glue; Return implode ($ separator, $ string ); } |
Example 3.
Convert an array to string storage and string retrieval to an array (serialize & unserialize)
Here is an example:
The code is as follows: |
|
// $ Id: test3.php, v 1.0 2011-6-7 goba Exp $ /** * Test the PHP storage value * * @ Author Lok */ $ Config = array ( 'Host' => 'localhost ', 'User _ name' => 'root ', 'Password' => '', 'Db _ name' => 'ecshop ', 'Charset' => 'utf8 ', 'Remark' => array ('note' => 'Note password security', 'author' => 'lok ') ); $ Str = serialize ($ config ); $ Arr = unserialize ($ str ); Echo '-------- str -------- '; Var_dump ($ str ); Echo' ------- Arr ---------- '; Var_dump ($ arr) ?> Result: -------- Str -------- String (221) "a: 6: {s: 4:" host "; s: 9:" localhost "; S: 9: "user_name"; s: 4: "root "; S: 8: "password"; s: 0 :""; S: 7: "db_name"; s: 8: "ecshop "; S: 7: "charset"; s: 4: "utf8 "; S: 6: "remark"; a: 2: {s: 4: "note"; s: 24: "Pay attention to password security "; S: 6: "author"; s: 3: "Lok ";}}" ------- Arr ---------- Array (6) {["host"] => string (9) "localhost" ["User_name"] => string (4) "root" ["Password"] => string (0 )"" ["Db_name"] => string (8) "emoishop" ["Charset"] => string (4) "utf8" ["Remark"] => array (2) {["note"] => string (24) "pay attention to password security" ["Author"] => string (3) "Lok "}} |
Transform (), implode () function to convert. today I saw a friend asking this question. let's sort it out...