Home  >  Article  >  Backend Development  >  Several methods for converting PHP strings into PHP arrays

Several methods for converting PHP strings into PHP arrays

不言
不言Original
2018-04-13 14:10:481932browse

This article introduces several methods of converting PHP strings into PHP arrays. Now I share them with you. Friends in need can take a look.

Reprinted from:

==================================


Today I saw a post from a friend in the PHP forum asking PHP how to convert a string into an array; as a PHP programmer, Xinyi's first reaction was to think of the two functions explode() and implode(). Shinichi also uses the function inside to convert it into an 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

The above is the string that netizens need to convert into a PHP array; the following is also Xinyi provides PHP conversion code


$v){
        $arr = explode('=',$v);
        $arr2[$k] = $arr[1];
}
print_r($arr2);
?>
//输出
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
)
//第二个
$v){
        $arr = explode('=',$v);
        $arr2[$arr[0]] = $arr[1];
}
print_r($arr2);
?>
//输出
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
)


Xinyi’s PHP code can also solve the problem of converting strings into arrays. Finally, phper netizens should find out in the manual that the parse_str() function can convert a string into a PHP array; let’s introduce the description of the parse_str() function so that you can have a good impression in your mind.
The parse_str() function converts the query Strings are parsed into variables.

parse_str(string,array)
string:需要转换的字符串;
array:返回到一个数组中;
如果没有设置array的话就覆盖同名变量

Related recommendations:

How to convert a PHP string into an array

How to add variables to a PHP string




The above is the detailed content of Several methods for converting PHP strings into PHP arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn