Home php教程 php手册 php操作字符串转换成数组

php操作字符串转换成数组

Jun 06, 2016 pm 08:09 PM
php string Replace with operate array forum

今天在php论坛中看到以为朋友发了一个帖子请教php如何把字符串转换成数组;作为php程序员的新一第一反应是联想到explode(),implode()这两个函数。新一也是用里面的函数进行转换成数组的。 con[1]=28selt[1]=1con[2]=29selt[2]=4con[3]=26selt[3]=4 con[4]=30s

今天在php论坛中看到以为朋友发了一个帖子请教php如何把字符串转换成数组;作为php程序员的新一第一反应是联想到explode(),implode()这两个函数。新一也是用里面的函数进行转换成数组的。

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

上面就是网友需要转换成PHP数组的字符串;下面也是新一提供PHP转换代码

<?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);
$arr2 = array();
foreach($arr as $k=>$v){
        $arr = explode('=',$v);
        $arr2[$k] = $arr[1];
}
print_r($arr2);
?&gt;
//输出
Array
(
    [0] =&gt; 28
    [1] =&gt; 1
    [2] =&gt; 29
    [3] =&gt; 4
    [4] =&gt; 26
    [5] =&gt; 4
    [6] =&gt; 30
    [7] =&gt; 2
    [8] =&gt; 4
    [9] =&gt; 1
    [10] =&gt; 11
    [11] =&gt; 12
)
//第二个
<?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);
$arr2 = array();
foreach($arr as $k=>$v){
        $arr = explode('=',$v);
        $arr2[$arr[0]] = $arr[1];
}
print_r($arr2);
?&gt;
//输出
Array
(
    [con[1]] =&gt; 28
    [selt[1]] =&gt; 1
    [con[2]] =&gt; 29
    [selt[2]] =&gt; 4
    [con[3]] =&gt; 26
    [selt[3]] =&gt; 4
    [con[4]] =&gt; 30
    [selt[4]] =&gt; 2
    [con[5]] =&gt; 4
    [selt[5]] =&gt; 1
    [con[6]] =&gt; 11
    [con[7]] =&gt; 12
)
Copy after login

新一的PHP代码也可以解决字符串转换成数组的问题。最后phper网友应该在手册上面查到了parse_str()函数就可以把字符串转换成PHP数组;下面介绍一下parse_str()函数的说明让自己在脑子中好有印象.
parse_str() 函数把查询字符串解析到变量中。

parse_str(string,array)
string:需要转换的字符串;
array:返回到一个数组中;
如果没有设置array的话就覆盖同名变量
Copy after login
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles