How to convert strings and arrays into one another? {1: & quot; 11 & quot;, 2: & quot; 22 & quot;, 3: & quot; 33 & quot;, 4: & quot; 44 & quot;, 7: & quot; 77 & quot ;} to convert the preceding string to an array (1 = & gt; '11', 2: how to convert the string and array
A string is as follows:
{1: "11", 2: "22", 3: "33", 4: "44", 7: "77 "}
To convert the above string to an array like this
Array (
1 => '11 ',
2 => '22 ',
3 => '33 ',
4 => '44 ',
7 => '77 ',
)
The index 11 22 33 44 77 of the array can access the corresponding value through $ arr [2 ].
After some operations are performed on the array value, the array becomes
Array (
1 => '18 ',
2 => '20 ',
3 => '33 ',
4 => '44 ',
6 => '66 ',
7 => '77 ',
18 => '76 ',
)
Then how can we convert this array to a string like this:
{1: "18", 2: "20", 3: "33", 4: "44", 6: "66", 7: "77", 18: "76 "}
------ Solution --------------------
PHP code
$ S = '{1: "11", 2: "22", 3: "33", 4: "44", 7: "77 "}'; $ s = preg_replace ('/"? (\ W + )"? /E ','"\"". trim ("$1 ","\""). "\" "', $ s); print_r (json_decode ($ s, 1 ));