Home >Backend Development >PHP Tutorial >php有没得 直接把字符串分割为key value形式的函数,该怎么处理

php有没得 直接把字符串分割为key value形式的函数,该怎么处理

WBOY
WBOYOriginal
2016-06-13 10:08:171087browse

php有没得 直接把字符串分割为key value形式的函数
有没有这样的函数把 a,100,b,32,c,10

变成 array(

 'a'=>100,
 'b'=>32,
 'c'=>10
)

有没得直接的函数
否则我只能str_split 然后循环变了 o(╯□╰)o

------解决方案--------------------

PHP code
$str = 'a,100,b,32,c,10,d,20';$res_str = implode(',',array_map(create_function('$v', 'return implode("=>",$v);'), array_chunk(explode(',', $str), 2)));$res =  eval("return array($res_str);");echo '<pre class="brush:php;toolbar:false">';print_r($res);echo '
';/*Array( [a] => 100 [b] => 32 [c] => 10 [d] => 20)*/
------解决方案--------------------
直接的函数没有,恶心的代码有很多...

PHP code
$Str = 'a,100,b,32,c,10';preg_match_all('/[a-z]+/i', $Str, $Aarray);$Aarray = $Aarray[0];preg_match_all('/[0-9]+/', $Str, $Barray);$Barray = $Barray[0];$Carray = array_combine($Aarray, $Barray);print_r($Carray);<br><font color="#e78608">------解决方案--------------------</font><br>11文 你又来鸟 我只想到 explode<br>
PHP code
$str = 'a,100,b,32,c,10';$arr = explode(',',$str);for($i = 0 ; $i <count if></count><font color="#e78608">------解决方案--------------------</font><br>来一个另类的<br>
PHP code
$s = 'a,100,b,32,c,10';preg_replace('/(\w+),([^,]+)/se', '$ar[$1]=$2', $s);print_r($ar);<div class="clear">
                 
              
              
        
            </div>
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