Home > Backend Development > PHP Tutorial > Object Array Understanding PHP JSON Arrays and Objects

Object Array Understanding PHP JSON Arrays and Objects

WBOY
Release: 2016-07-28 08:25:55
Original
718 people have browsed it

During the interaction between the PHP backend and the client data, the JSON data is sometimes in an uncertain format. Sometimes it is an array, sometimes it is an object, which makes the client developer feel like it is about to collapse.

Therefore, it is the most important step for front-end and back-end related personnel to have a necessary understanding of the principle of PHP's json_encode function.

The array in PHP is a universal data structure. Unlike other languages ​​that define many restrictive data types to describe the structure according to the required scenarios, it is difficult for PHP programmers to clearly explain to the client what is returned. Structured data.

It becomes a situation where the array data is obviously encoded into json through PHP, but the output value is sometimes an array and sometimes an object.

<code><span><?php</span><span>/*</span><span>如果你想生成一个json格式的数组格式(Vector)而非对象格式(Map)的,那么数据的下标:</span><span>必须是数字索引,</span><span>必须从0开始,</span><span>必须从小到大依次增加、中间不可以跳跃、顺序不可变动.</span><span>*/</span><span>//符合数组</span><span>$vector</span> = <span>[</span><span>12</span><span>,</span><span>23</span><span>,</span><span>18</span><span>];</span><span>echo</span><span>json_encode</span><span>(</span><span>$vector</span><span>);</span><span>//符合数组</span><span>$vector2</span> = <span>[</span><span>0</span> => <span>12</span><span>,</span><span>1</span> => <span>23</span><span>,</span><span>2</span> => <span>18</span><span>];</span><span>echo</span><span>json_encode</span><span>(</span><span>$vector2</span><span>);</span><span>//不符合数组,下标有跳跃</span><span>$map</span> = <span>[</span><span>0</span> => <span>12</span><span>,</span><span>1</span> => <span>23</span><span>,</span><span>2</span> => <span>18</span><span>,</span><span>4</span> => <span>20</span><span>];</span><span>echo</span><span>json_encode</span><span>(</span><span>$map</span><span>);</span><span>//不符合数组,下标顺序不对</span><span>$map</span> = <span>[</span><span>0</span> => <span>12</span><span>,</span><span>1</span> => <span>23</span><span>,</span><span>3</span> => <span>18</span><span>,</span><span>2</span> => <span>20</span><span>];</span><span>echo</span><span>json_encode</span><span>(</span><span>$map</span><span>);</span><span>//不符合数组,下标没有从0开始</span><span>$map_1</span> = <span>[</span><span>1</span> => <span>'111'</span><span>,</span><span>2</span> => <span>'asdfa'</span><span>];</span><span>echo</span><span>json_encode</span><span>(</span><span>$map_1</span><span>);</span><span>//典型的对象格式</span><span>$map_2</span> = <span>[</span><span>'abc'</span> => <span>1</span><span>,</span><span>'de'</span> => <span>2</span><span>,</span><span>'fi'</span> => <span>null</span><span>];</span><span>echo</span><span>json_encode</span><span>(</span><span>$map_2</span><span>);</span><span>/**</span><span> * 请注意上面的变量命名,对应于Hack中的概念,Vector和Map为两种数据集合类型</span><span> * 如果你需要将"索引数组"强制转化成"对象",可以这样写</span><span> * json_encode((object) $a);</span><span> * 或</span><span> * json_encode ($a, JSON_FORCE_OBJECT);</span><span> *</span><span> */</span></span></code>
Copy after login

What is the solution?

It is a class that encapsulates the two data structures of Vector and Map through PHP. When constructing the json output structure, these two types are used for encapsulation and conversion to ensure the certainty of the structure.

The above introduces the understanding of object arrays, PHP JSON arrays and objects, including the content of object arrays. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template