Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize json to Array by php

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article "php how to achieve json rotation array" in addition to programmers, most people do not quite understand, today Xiaobian in order to make you a better understanding of "php how to achieve json rotation array", summed up the following content, with a certain reference value, the detailed steps of the content is clear, the details are handled properly, I hope you can get something through this article, let's take a look at the specific content.

Php to achieve json transfer array method: 1, create a PHP sample file; 2, define a JSON data; 3, through the "json_decode ($json,true)" method to convert json into an array.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does php implement json to an array?

Json_decode ()

This function is used to convert json text into the corresponding PHP data structure.

Here is an example:

$json ='{"foo": 12345}'; $obj = json_decode ($json); print $obj- > {'foo'}; / / 12345

Normally, json_decode () always returns a PHP object, not an array. For example:

$json ='{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}'; var_dump (json_decode ($json))

The result is a PHP object:

Object (stdClass) # 1 (5) {["a"] = > int (1) ["b"] = > int (2) ["c"] = > int (3) ["d"] = > int (4) ["e"] = > int (5)}

If you want to force the generation of PHP associative arrays, json_decode () needs to add a parameter, true:

$json ='{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}'; var_dump (json_decode ($json,true))

The result is an associative array:

Array (5) {["a"] = > int (1) ["b"] = > int (2) ["c"] = > int (3) ["d"] = > int (4) ["e"] = > int (5)}

The following three ways of writing json are all wrong. Can you see where they are wrong?

Common errors of json_decode ()

$bad_json = "{'bar':' baz'}"; $bad_json ='{bar: "baz"}'; $bad_json ='{"bar": "baz",}'

The first mistake is that the json delimiter (delimiter) allows only double quotes, not single quotes. The second error is the "first name" of the json name-value pair (the part to the left of the colon), and double quotation marks must be used in all cases. The third error is that you cannot add a comma (trailing comma) after the last value. Executing json_decode () on all three strings returns null with an error.

In addition, json can only be used to represent objects (object) and arrays (array), and if you use json_decode () for a string or numeric value, null will be returned.

Var_dump (json_decode ("Hello World")); / / what language is nullphp php, a nested acronym that stands for English Hypertext preprocessing language (PHP:Hypertext Preprocessor). PHP is a kind of HTML embedded language. PHP is somewhat similar to Microsoft's ASP. It is a scripting language that embeds HTML documents on the server. The style of the language is similar to C language, and now it is widely used by many website programmers.

Thank you for your reading. I hope you have a certain understanding of the key issue of "how to achieve json rotation array with php". The specific usage still needs to be understood through hands-on experiments. Try it quickly. If you want to read more articles about related knowledge points, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report