How to implement a PHP string conversion array

Source: Internet
Author: User
This article to share the content is how to implement PHP string conversion array, as a new PHP programmer's first reaction is Lenovo to explode (), implode () these two functions. The new one is also used to convert the function inside the array. has a certain reference value, the need for friends can refer to

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 user needs to convert to PHP array string; The following is a new PHP conversion code provided

<?php$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); $arr 2 = Array (        ), foreach ($arr as $k + $v) {$arr = explode (' = ', $v); $arr 2[$k] = $arr [1];} Print_r ($arr 2);? >//output Array ([0] = [1] = 1 [2] = [3] = 4 [4] = [5] = 4 [6] = 3 0 [7] = 2 [8] = 4 [9] = 1 [Ten] = [one] = 12)//Second &LT;?PHP$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); $arr 2 = Array (); foreach ($arr as $k + = $v) {$arr = E        Xplode (' = ', $v); $arr 2[$arr [0]] = $arr [1];} Print_r ($arr 2);?    >//output Array ([con[1]] [[selt[1]] + 1 [con[2]] [selt[2]] = 4 [con[3]] = 26 [Selt[3]] + 4 [con[4]] [[selt[4]] = 2 [con[5]] [4] [selt[5]] + 1 [con[6]] = [con[ 7]] = 12)

The new PHP code can also solve the problem of string conversion to an array. Finally, Phper netizens should find the PARSE_STR () function on the manual to convert the string into a PHP array; here's a description of the PARSE_STR () function to make a good impression in your mind.
The Parse_str () function parses the query string into a variable.

Parse_str (String,array) string: strings to be converted; array: returns to an array, overwriting a variable with the same name if no array is set

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.