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

Object Array Understanding PHP JSON Arrays and Objects

Jul 28, 2016 am 08:25 AM
object array

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>&lt;?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> =&gt; <span>12</span><span>,</span><span>1</span> =&gt; <span>23</span><span>,</span><span>2</span> =&gt; <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> =&gt; <span>12</span><span>,</span><span>1</span> =&gt; <span>23</span><span>,</span><span>2</span> =&gt; <span>18</span><span>,</span><span>4</span> =&gt; <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> =&gt; <span>12</span><span>,</span><span>1</span> =&gt; <span>23</span><span>,</span><span>3</span> =&gt; <span>18</span><span>,</span><span>2</span> =&gt; <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> =&gt; <span>'111'</span><span>,</span><span>2</span> =&gt; <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> =&gt; <span>1</span><span>,</span><span>'de'</span> =&gt; <span>2</span><span>,</span><span>'fi'</span> =&gt; <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.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles