How PHP converts such a string into an array

Source: Internet
Author: User
PHP converts this string into an array. this post is edited by tangl198710 at 12:11:55.

The string is as follows:

{'Aid ': '21', 'ctl _ a_cpu': 'df ', 'ctl _ a_ram': 'df ', 'ctl _ a_disk': 'df ', 'ctl _ a_fip': 'df ', 'ctl _ a_ OS': 'c _ 1_2 ', 'ctl _ a_ OS _lang': 'c _ 4_1, c_4_3 ', 'Comment ': 'other requirements \ ', \ 'Haha \', \ '', 'total': '000000', 'typeid': '6 '}

How can I convert such a string into an array?

Convert the above string to an array: What is the value after the subscript before the number?


Reply to discussion (solution)

$ Str = <
  
  

Does json_decode pay special attention to using this function? I printed it blank and found other solutions. But I want to know how to use the json_decode function.

Your format is incorrect.
I don't know what tool you use to output json data.

Let's take a look at this example.

$ Str1 = '{"a": 21 }';
$ Str2 = "{\" B \ ": 21 }";
$ Str3 = "{'C': 21 }";
Print_r (json_decode ($ str1, true ));
Print_r (json_decode ($ str2, true ));
Print_r (json_decode ($ str3, true ));


The output result is Array ([a] => 21) Array ([B] => 21)

The third print_r output is blank.

Json_decode (string $ json [, bool $ assoc])
Accept a JSON string and convert it to a PHP variable

Parameters
Json
A string in json string format.

Assoc
If this parameter is set to TRUE, array instead of object is returned.


Same as above, directly use the json_decode string.
Write as follows:
Json_decode ($ str, true );
Note that if "true" is not added, the object is converted into an array.

Well .. As a result, json_decode () cannot be directly used for processing. the manual conversion method is changed:

$ Str = <
   
    
'; Print_r ($ result );
   


PHP array basics
Http://3aj.cn/php/39.html

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.