How to simplify the code

Source: Internet
Author: User
{A: 1, B: 2, c: 3} is a string similar to this. If a is input, 1 is returned. If B is input, 2 is returned .. And so on. I wrote a code myself, but it looked too redundant. My idea is to write him into an array. Then return. For simpler code, {code...} {a: 1, B: 2, c: 3} is a string similar to this. If a is input, 1 is returned. If B is input, 2 is returned .. And so on.
I wrote a code myself, but it looked too redundant.
My idea is to write him into an array. Then return. For simpler code,


  "1","b"=>"2","c"=>"3") */$str="{a:1,b:2,c:3}";$search=array("{","}");$str=str_replace($search, "", $str);$arr=explode(",", $str);$arr1=array();for ($i=0;$i
  

Reply content:

{A: 1, B: 2, c: 3} is a string similar to this. If a is input, 1 is returned. If B is input, 2 is returned .. And so on.
I wrote a code myself, but it looked too redundant.
My idea is to write him into an array. Then return. For simpler code,


   "1","b"=>"2","c"=>"3") */$str="{a:1,b:2,c:3}";$search=array("{","}");$str=str_replace($search, "", $str);$arr=explode(",", $str);$arr1=array();for ($i=0;$i
   

The example is too json, so it is certainly the best to make the source into a standard json.

It must be like this. Regular Expressions are easier to write.

$str="{a:1,b:2,c:3}";preg_match_all('/(\w+):(\d+)/', $str, $matches);$arr = array_combine($matches[1], $matches[2]);var_dump($arr);

$ Str = trim ("{a: 1, B: 2, c: 3}", '{}'); // trim: $ arr = explode (", ", $ str); $ arr1 = array (); for ($ I = 0, $ l = count ($ arr); $ I <$ l; $ I ++) {$ _ arr = explode (":", $ arr [$ I]); $ arr1 [$ _ arr [0] = $ _ arr [1];} print_r ($ arr1 );

To simplify it, modify your original string. Your original string is also json, but the format is not very good. The standard format is similar to {"a": 1, "B": 2, "c": 3}. php has strict requirements on json strings, while js is loose.
In this case, $ arr = json_decode ('{"a": 1, "B": 2, "c": 3}'); that is, a = 1, B = 2, c = 3 array.

Isn't the "string" JSON? Do you want to write a JSON parser?

function foo($str, $key){    $obj = json_decode($str, true);    return $obj[$key];}

This is not a json file. You can use the json_decode file in php to parse it.

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.