Describe in detail the differences between js and php object arrays and the use of json

韦小宝
Release: 2018-03-12 17:01:24
Original
2861 people have browsed it

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"; 获取对象操作:点访问发或中括号访问法
Copy after login

JS array structure:

定义一个数组方法1 :var bbb = ["齐天大圣","猪八戒","沙僧","唐三藏"] 定义一个数组方法2 var ccc = new Array(); ccc[0] = "孙悟空"; ccc[1] = "如意金箍棒";
Copy after login

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对象的访问:通过符号"->"。
Copy after login

php array:

创建数组有两种方式: 方式一 使用赋值方式定义数组: $arr[]="PHP"//默认下标从0开时。0有值则看1有没有值 $arr[3]="c语言" $arr["XINGMING"]="孙悟空" 方法二:通过array()函数。 $arr = array("id"=>1,"name"=>"悟空");//注意此时数组的写法。 PHP数组有2种: 索引数组:下标为整形 关联数组: 下标为字符串 通过中括号访问数组数据
Copy after login

js operation string

通过js中的JSON对象JSON_stringify/JSON_parse操作字符串。 JSON_stringify 将js对像转换成JSON字符串。 JSON_parse 将JSON字符串转换成js对象,这样我就访问对象中的属性和方法。
Copy after login

php operation string

json_encode 将php对象编码成json字符串的形式。 json_decode 将json字符串转换成对象或数组,为true转换成数组形式。
Copy after login

Important note

1. In php, for the effect of printing arrays or objects through var_dump, please see the following code:

 效果如下: 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这个函数本身有关,这个函数规定数组和对象的数据就通过这种方式打印。
Copy after login

2. The format of json string

JSON的正确格式为:$A = '{"NAME":"LIUXUSJ"}'; 如果写成$A = "{"NAME":"LIUXUNA"}"会显示错误。php或者js在识别代码的时候会从左至右依次解读,这样它会首先识别"{"而不是"NAME",所以最外围最好用单引号包裹。
Copy after login

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!