Php uses json_decode to forcibly convert json data to an array

Source: Internet
Author: User

A simple example of php using json_decode to forcibly convert json data into arrays. We use var_dump (json_decode ($ str, true )); convert json to the data we want.

The Code is as follows: Copy code

$ A ['D'] [] = 1;
$ A ['D'] [] = 2;
Echo $ str = json_encode (array ($ ));
Var_dump (json_decode ($ str ));

Conversion code

The Code is as follows: Copy code
Array (1 ){
[0] =>
Object (stdClass) #1 (1 ){
["D"] =>
Array (2 ){
[0] =>
Int (1)
[1] =>
Int (2)
}
}
}

As you can see, this is an object in an array;
We force the json_decode result to be converted to an array. -- add the parameter to the fourth row.

The Code is as follows: Copy code

Var_dump (json_decode ($ str, true ));

Array (1 ){
[0] =>
Array (1 ){
["D"] =>
Array (2 ){
[0] =>
Int (1)
[1] =>
Int (2)
}
}
}


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.