


Describe in detail the differences between js and php object arrays and the use of json
In daily development, everyone knows that JS objects and arrays are similar to PHP objects and arrays, but there are still some differences. Students who don’t understand the differences between JS and PHP objects and arrays can take a look together. This article also covers the use of json data! Without further ado, let’s take a look!
JS object structure:
创建一个对象: 方法一 var aaa = { firsetname:"liu", lastname:"xuan", class:four, down:function(){} }; 方法二 通过构造函数创建对象。 var aaaa = new Object();//object()就是js内置的一个构造函数,与new结合就可以创建一个新对象。 添加属性:aaaa.firstname = "liu"; aaaa.lastname = "xuan"; 获取对象操作:点访问发或中括号访问法
JS array structure:
定义一个数组方法1 :var bbb = ["齐天大圣","猪八戒","沙僧","唐三藏"] 定义一个数组方法2 var ccc = new Array(); ccc[0] = "孙悟空"; ccc[1] = "如意金箍棒";
Remember here that arrays in js are index arrays, and arrays are a special object.
php objects:
Instances of classes in php are objects. First we need to define a class and then instantiate the class to create an object.
class qqq{ var $a; var $b; public function c(){} } $d = new qqq; php对象的访问:通过符号"->"。
php array:
创建数组有两种方式: 方式一 使用赋值方式定义数组: $arr[]="PHP"//默认下标从0开时。0有值则看1有没有值 $arr[3]="c语言" $arr["XINGMING"]="孙悟空" 方法二:通过array()函数。 $arr = array("id"=>1,"name"=>"悟空");//注意此时数组的写法。 PHP数组有2种: 索引数组:下标为整形 关联数组: 下标为字符串 通过中括号访问数组数据
js operation string
通过js中的JSON对象JSON_stringify/JSON_parse操作字符串。 JSON_stringify 将js对像转换成JSON字符串。 JSON_parse 将JSON字符串转换成js对象,这样我就访问对象中的属性和方法。
php operation string
json_encode 将php对象编码成json字符串的形式。 json_decode 将json字符串转换成对象或数组,为true转换成数组形式。
Important note
1. In php, for the effect of printing arrays or objects through var_dump, please see the following code:
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); var_dump(json_decode($json, true)); ?> 效果如下: object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } //有人可能会纠结除了显示的数据类型不同外,打印结果好像没有什么差别。其实数据的显示格式与打印成数组还是对象无关,只与var_dump这个函数本身有关,这个函数规定数组和对象的数据就通过这种方式打印。
2. The format of json string
JSON的正确格式为:$A = '{"NAME":"LIUXUSJ"}'; 如果写成$A = "{"NAME":"LIUXUNA"}"会显示错误。php或者js在识别代码的时候会从左至右依次解读,这样它会首先识别"{"而不是"NAME",所以最外围最好用单引号包裹。
3. Single quotes and double quotes can wrap each other. However, in PHP, variables in single quotes will not be parsed, only variables in double quotes will be parsed.
Related articles:
js Define object array (combination) multi-dimensional array method
How to convert JSON data format to PHP array
The above is the detailed content of Describe in detail the differences between js and php object arrays and the use of json. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
