Home > Article > Backend Development > PHP basic study notes (4)
Introduction to Arrays
Concept: It is a collection of several data put together in a certain order, which is called an "array" as a whole. An array is an ordered collection of data.
Definition form:
var arr1 = new Array(1, 5, 8, 7, 2, 10); //定义了一个数组,其中具有6个数据 var arr2 = new Array(); //只是单纯地定义了一个数组(名),但没有给值(数据),即现在是空的 var arr3 = [1, 5, 8, 7, 2, 10]; //同arr1,只是一种简写的定义法。 var arr4 = [ ]; //同arr2,也是一个空数组。
The use of arrays: The so-called use actually refers to the use of each item of the array.
Value:
var v1 = arr1[0]; //取得数组arr1中的第一项,0叫做下标 var v2 = arr3[3] + 10; //取得数组arr3中的第4项,4叫做下标 ——所谓下标,其实就是数组的每一个数据的“顺序号”——从0开始编号,是连续的整数。
Assignment:
arr1[0] = 10;
arr2[0] = 22; arr2[1] = 33.3; arr2[2] = “444”; arr2[3] = “abc”; arr2[4] = true;
//At this time, the array arr2 is equivalent to this: [ 22, 33.3, “444”, “abc”, true ]
The "visual image" of the array (taking arr3 as an example):
下标值: 0 1 2 3 4 5 数据值: 1 5 8 7 2 10
The syntax to obtain the length of an array - that is, the number of data in it is:
var v1 = array name.length;
的 Special attention: The maximum bidding of the array is the length of the array decreased by 1.Usual pattern for array traversal:
var len = array name.length;
for(var i = 0; i < len; i++)
{
//Here is the processing of each item of the array, the writing method of each item is: Array name [i]
}
Another form of array traversal - for in loop statement.
for( var v1 in array name arr1 )
{
//Here is the loop body, which is a traversal loop specifically for the array arr1, where the value of v1 is the subscript value representing each item of the array.
// v1 is just a "temporary variable", representing each subscript, which will change from 0 to the maximum subscript of the array.
}
"two-dimensional" array:
var v1 = [2, 5, 1, 5]; var v2 = [5, 1, 6, 8]; var v3 = [8, 0, 9, 7]; var v4 = [v1, v2, v3]; var v5 = [ [2, 5, 1, 5], [5, 1, 6, 8], [8, 0, 9, 7] ];
——There is actually no difference between v4 and v5. Both of them can be called "two-dimensional arrays".
Operations on "two-dimensional" array elements:
Value:
V VAR S1 = v5 [0] [1]; // 5 // Equivalent to get the second item of the first item of the V5 array (this is still an array).
var s2 = v5[2][3] + 100; //107
Assignment:
v5[0][1] = 200;
v5[2][3] = 300;
Common methods of array objects:
What is a method: A method is actually a function! ——Just if a function "belongs to" an "object", then the function is called a method of the object.
function maibao(){ document.write(“啦啦啦,我是卖报的小行家,卖报啦卖报啦。”); } var myDreamGirl = { name: “小花”, age:18, edu:”大学”, sex:”女”, nengli1: function (){ document.write(“洗衣!”); } , nengli2: function (){ document.write(“做饭!”); } , nengli3: maibao }; var v1 = [2, 5, 1, 5]; var v2 = [5, 1, 6, 8];
Strictly speaking, an array is also an object - even strings are objects.
v1 as an object has properties and methods:
Attributes:
.
Method:On A certain array.concat (other array): connect two arrays into a new "longer" array.
var s1 = v1.concat( v2 ); //At this time, s1 is an array like this: [2, 5, 1, 5, 5, 1, 6, 8];
An array.join ("string"): "Concatenate" all the items in the array with the specified characters to become a "long" string.
某数组.pop(); //将该数组的最后一项“移除”(删除),并返回该项数据,即该数组少了一项
var s3 = v1.pop(); //结果v1只剩这个:[2,5,1]; s3的值是5
某数组.push(新数据项d1); //将新的数据d1添加到该数组的最后位置,即数组多了一项。
var s4 = v1.push( 55 ); //v1此时为:[2,5,1, 55], s4的值为新数组的长度,即4
某数组.shift(); //将该数组的第一项“移除”(删除),并返回该项数据,即该数组少了一项
var s5 = v1.shift(); //结果v1只剩这个:[5, 1,55]; s5的值是2
某数组.unshift(新数据项d1); //将新的数据d1添加到该数组的最前位置,即数组多了一项。
var v6 = v1.unshift( 66 ); //v1此时为:[66, 5, 1, 55], s6的值为新数组的长度,即4
javascript语言是一门基于对象的语言。
字符串对象:
var str1 = new String(“abcdefgabc”); //这是一个“字符串对象” var str2 = “abcdefgabc”; //这个字符串跟前面str1几乎没有区别 字符串对象的属性: .length——获得一个字符串的长度(也就是字符个数) 字符串对象的方法: 1. str1.charAt( n ); ——获得字符串str1中位置为n的那个字符(字符的位置也是从0开始算起)var s1 = str1.charAt( 3 ); //s1的结果是:”d” 2. str1.toUpperCase(); ——获取str1全部转换为大写的结果 var s2 = str1.toUpperCase(); //s2的结果是:”ABCDEFGABC” 3. str1.toLowerCase(); ——获取str1全部转换为小写的结果 var s3 = str1.toLowerCase(); //s3的结果是:”abcdefgabc” 4. str1.replace(“字符1”, “字符2”); ——将str1中的“字符1”替换为“字符2” var s4 = str1.replace(“cd”, “999”); //s4的结果是:”ab999efgabc” 5. str1.indexOf(“字符1”); ——获得“字符1”在str1中第一次出现的位置,如果没有出现,结果是-1 var s5 = str1.indexOf(“ab”); //s5的结果是0 6. str1.lastIndexOf(““字符1”); ——获得“字符1”在str1中最后一次出现的位置,如果没有出现,结果是-1 var s6 = str1.lastIndexOf(“ab”); //s6的结果是7 7. str1.substr(n, m ) ——取得str1中从位置n开始的m个字符,m可以省略,则表示从位置n一直取到字符串的最后——注意,这种“取”并不影响str1这个原始字符 var s7 = str1.substr(2, 4); //s7为:”cdef” 8. str1.substring( n, m )——取得str1中从位置n到位置m的前一个字符。 var s8 = str1.substring(2, 4); //s8为:”cd” 9. str1.split(“字符1”) ——将str1以指定的“字符1”为分界,分割成一个数组,结果是一个数组 var s9 = str1.split(“b”); //s9的结果是一个数组:[“a”, “cdefga”, “c”]
Math对象
Math对象是一个系统内部定义的对象,我们无需去“新建一个Math对象”——跟string对象和array对象不同。即Math对象是直接使用的。
我们学习Math对象,无非是学习一些常见的数学处理函数——这里当就叫做方法了:
属性:
Math.PI——代表圆周率这个“常数”
方法:
1. Math.max(数值1,数值2,…..) ——求得若干个数值中的最大值。 2. Math.min(数值1,数值2,…..) ——求得若干个数值中的最小值。 3. Math.abs( 数值1) ——求得数值1的绝对值 4. Math.pow( x,y) ——求得数值x的y次方,也就是“幂运算” 5. Math.sqrt( x ) ——求得x的开方 6. Math.round( x ) ——求得x的四舍五入的结果值; 7. Math.floor( x ) ——求得x的向下取整的结果,即找到不大于x的一个最大的整数。 a) Math.floor( 3.1 ) ? 3 b) Math.floor( 3.8 ) ? 3 c) Math.floor( 3 ) ? 3 d) Math.floor( -3.1 ) ? -4 e) Math.floor( -3.8 ) ? -4 8. Math.ceil( x ) ——求得x的向上取整的结果,即找到不小于x的一个最小的整数 a) Math.floor( 3.1 ) ? 4 b) Math.floor( 3.8 ) ? 4 c) Math.floor( 3 ) ? 3 d) Math.floor( -3.1 ) ? -3 e) Math.floor( -3.8 ) ? -3 9. Math.random() ——获得一个0~1范围中的随机“小数”,含0,但不含1 //获得两个整数(n1,n2)之间的随机整数的通用做法: var v1 = Math.random() v1 = v1 * (n2-n1+1); v1 = Math.floor( v1 ) + n1; //此时v1就是结果 //将上述3步写出一步就是(通用公式): var v1 = Math.floor( Math.random() * (n2-n1+1) ) + n1
以上就介绍了php基础学习笔记(4),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。