: This article mainly introduces php basic learning notes (4). If you are interested in the PHP Tutorial, refer to it. Array introduction
Concept: it is a collection of several data in a certain order, which is called an "array" as a whole ". An array is a set of ordered data columns.
Definition form:
Var arr1 = new Array (1, 5, 8, 7, 2, 10); // defines an Array with six data var arr2 = new Array (); // only an array (name) is defined, but no value (data) is given. that is, var arr3 = [1, 5, 8, 7, 2, 10]; // same as arr1, it is just a short definition. Var arr4 = []; // same as arr2, it is also an empty array.
Array usage: the so-called use actually refers to the use of each item of the array.
Valid value:
Var v1 = arr1 [0]; // obtain the first entry in array arr1. 0 is called subscript var v2 = arr3 [3] + 10; // obtain the first entry in array arr3, 4 is called subscript-the so-called subscript, which is actually the "ordinal number" of each data in the array-starting from 0, which is a continuous integer.
Assignment:
Arr1 [0] = 10; // assign a value to the first entry of array arr1 as 10. in this case, it is equivalent to modifying the value of arr1. It can also be said that a variable is assigned a value again.
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 the following: [22, 33.3, "444", "abc", true]
The "visual image" of the array (taking arr3 as an example ):
Value: 0 1 2 3 4 5 data value: 1 5 8 7 2 10
Get the length of an array-the syntax for the number of data is:
Var v1 = array name. length;
Note: The maximum subscript of the array is the length of the array minus 1.
The normal pattern of array traversal:
Var len = array name. length;
For (var I = 0; I <len; I ++)
{
// Here is the processing of each item of the array. each item is written as an array name [I].
}
Another form of array traversal is the for in loop statement.
For (var v1 in array name arr1)
{
// Here is the loop body, which is used to traverse the array arr1. the v1 value indicates the lower value of each item of the array.
// V1 is just a "temporary variable", representing each subscript, which changes from 0 to the maximum subscript of the array in turn.
}
"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 no difference between v4 and v5. both of them can be called "two-dimensional arrays".
Operations on two-dimensional array elements:
Valid value:
Var s1 = v5 [0] [1]; // 5 // It is equivalent to obtaining 2nd of the first item (this is an array) in the v5 array.
Var s2 = v5 [2] [3] + 100; // 107
Assignment:
V5 [0] [1] = 200;
V5 [2] [3] = 300;
Common methods for array objects:
What is a method: a method is actually a function! -- If a function "belongs to" an object ", it is called the method of the object.
Function maibao () {document. write .");} Var myDreamGirl = {name: "Xiaohua", age: 18, edu: "", sex: "female", nengli1: function () {document. write ("laundry! "); }, Nengli2: function () {document. write ("cook !"); }, Nengli3: maibao}; var v1 = [2, 5, 1, 5]; var v2 = [5, 1, 6, 8];
Strictly speaking, arrays are also an object-and even strings are objects.
V1 acts as an object and has attributes and methods:
Attribute:
An array. length: indicates the length of the array object.
Method:
An array. concat (other arrays): connects two arrays to form 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"): concatenates all items in the array with the specified character "string" to become a "long" string.
Vars2 = v1.join ("//"); // result s2 is the string "2 // 5 // 1 // 5"
An array. pop (); // "remove" (delete) the last item of the array and return the data, that is, the array has one fewer item.
Var s3 = v1.pop (); // The result v1 only contains this: [, 1]; the s3 value is 5
An array. push (new data item d1); // add the new data d1 to the last position of the array, that is, the array has one more item.
Var s4 = v1.push (55); // v1: [2, 5, 1, 55] At this time. the s4 value is the length of the new array, that is, 4
An array. shift (); // remove (delete) the first item of the array and return the data, that is, the array has one fewer item.
Var s5 = v1.shift (); // result v1 only has this: [5,]; s5 value is 2
An array. unshift (new data item d1); // add the new data d1 to the front position of the array, that is, an array with one more entry.
Var v6 = v1.unshift (66); // v1: [66, 5, 1, 55]. the s6 value is the length of the new array, that is, 4.
Javascript is an object-based language.
String object:
Var str1 = new String ("abcdefgabc"); // This is a "String object" var str2 = "abcdefgabc "; // This string has almost no difference with the previous str1 character string object attributes :. length -- obtain the length (the number of characters) of a string object: 1. str1.charAt (n); -- obtain the character whose position is n in str1 (the character position also starts from 0) var s1 = str1.charAt (3 ); // the result of s1 is: "d" 2. str1.toUpperCase (); -- obtain the result of converting all str1 values to uppercase. var s2 = str1.toUpperCase (); // the result of s2 is: "ABCDEFGABC" 3. str1.toLowerCase (); -- get the result of converting all str1 to lowercase var s3 = str1.toLowerCase ();/ /S3: "abcdefgabc" 4. str1.replace ("character 1", "Character 2"); -- replace "character 1" in str1 with "character 2" var s4 = str1.replace ("cd ", "999"); // s4 result: "ab999efgabc" 5. str1.indexOf ("character 1"); -- obtain the position where "character 1" appears for the first time in str1. if it does not appear, the result is-1var s5 = str1.indexOf ("AB "); // the result of s5 is 06. str1.lastIndexOf ("" character 1 "); -- obtains the position where" character 1 "last appeared in str1. if it does not appear, the result is-1var s6 = str1.lastIndexOf ("AB"); // the result of s6 is 77. str1.substr (n, m) -- Gets the m characters starting from position n in str1. if m can be omitted, it indicates that the string is obtained from position n. The last -- note that this "fetch" does not affect the original character var s7 = str1.substr (2, 4); // s7 is: "cdef" 8. str1.substring (n, m) -- obtain the first character in str1 from position n to position m. Var s8 = str1.substring (2, 4); // s8: "cd" 9. str1.split ("character 1") -- splits str1 into an array based on the specified "character 1". The result is an array var s9 = str1.split ("B "); // the result of s9 is an array: ["a", "cdefga", "c"]
Math object
The Math object is defined in the system. you do not need to create a new Math object. it is different from the string object and the array object. That is, the Math object is directly used.
Learning Math objects is nothing more than learning some common mathematical processing functions-here we call it a method:
Attribute:
Math. PI -- representing the "constant" of the circumference rate"
Method:
1. Math. max (value 1, value 2 ,.....) -- Obtain the maximum value among several values. 2. Math. min (value 1, value 2 ,.....) -- Obtain the minimum value of several values. 3. math. abs (numerical value 1) -- obtain the absolute value of numerical value 1 4. math. pow (x, y) -- obtain the power y of the value x, that is, "power operation" 5. math. sqrt (x) -- obtain the Square 6 of x. math. round (x) -- returns the result of rounding x. 7. math. floor (x) -- returns the rounded down result of x, that is, a maximum integer not greater than x. A) Math. floor (3.1 )? 3b) Math. floor (3.8 )? 3c) Math. floor (3 )? 3d) Math. floor (-3.1 )? -4e) Math. floor (-3.8 )? -48. Math. ceil (x) -- obtain the rounded up result of x, that is, find a smallest integer a) Math. floor (3.1 )? 4b) Math. floor (3.8 )? 4c) Math. floor (3 )? 3d) Math. floor (-3.1 )? -3e) Math. floor (-3.8 )? -39. Math. random () -- get a 0 ~ Random "decimal" in the range of 1, which contains 0 but does not include 1 // general practice for obtaining random integers between two integers (n1, n2): var v1 = Math. random () v1 = v1 * (n2-n1 + 1); v1 = Math. floor (v1) + n1; // v1 is the result at this time // write the above three steps (general formula): var v1 = Math. floor (Math. random () * (n2-n1 + 1) + n1
The above introduces the basic learning notes for php (4), including the content, and hope to be helpful to friends who are interested in PHP tutorials.