How to convert strings and arrays

Source: Internet
Author: User
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 ));

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.