Php has a function that does not directly split the string into keyvalue. how can this problem be solved?

Source: Internet
Author: User
Php does not have to directly split the string into keyvalue form. Is there such a function that converts a, 100, B, 32, c, 10 to array (& nbsp; a & gt; 100, & nbsp; B & gt; 32, & nbsp; c & gt; 10) There are no direct functions. Otherwise, I can only str_split and then change the o (bytes □splits) cyclically) o ------ solution ------------ php has no function that directly splits the string into key values.
Is there such a function that calls a, 100, B, 32, c, 10?

To array (

& Apos; a & apos; = & apos; 100 & apos,
'B' => 32,
'C' => 10
)

There are no direct functions available
Otherwise, I can only split the str_split and then change the cycle to o (else □else) o.

------ Solution --------------------
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'
';print_r($res);echo '
';/* Array ([a] => 100 [B] => 32 [c] => 10 [d] => 20 )*/
------ Solution --------------------
No direct functions, and a lot of disgusting code...

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 );
------ Solution --------------------
11 You Come Again, I only want to explode
PHP code
$ Str = 'a, 100, B, 32, c, 10'; $ arr = explode (',', $ str); for ($ I = 0; $ I
      ------ Solution --------------------
Come to an alternative
PHP code
$s = 'a,100,b,32,c,10';preg_replace('/(\w+),([^,]+)/se', '$ar[$1]=$2', $s);print_r($ar);

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.