- $number = Array (1,3,5,7,9);
- $color =array ("Red", "Blue", "green");
- $student = Array ("name", 17)
- ?>
Copy CodeExample 2:
- $language = Array (1=> "PHP",3=> "JAVA",4=> "C");
- $student = Array ("name" = "Zhang San", "Age" =>17)
- ?>
Copy CodeOf course, no value in the array is allowed, that is, an empty array:
- $result = Array ();
- ?>
Copy Code2. Using the Compact () function to create an array the compact () function in PHP can convert one or more variables to arrays Definition Format: Array compact (VAR1,VAR2 ...) Any string that has no variable name corresponding to it is skipped.
- $firstname = "Peter";
- $lastname = "Griffin";
- $age = "38";
- $result = Compact ("FirstName", "LastName", "age");
- Print_r ($result);
- ?>
Copy CodeOutput: Array ([FirstName] = Peter [LastName] = Griffin [age] + 38) Use a string with no corresponding variable name, and a variable an array group
- $firstname = "Peter";
- $lastname = "Griffin";
- $age = "38";
- $name = Array ("FirstName", "LastName");
- $result = Compact ($name, "location", "age");
- Print_r ($result);
- ?>
Copy CodeOutput: Array ([FirstName] = Peter [LastName] = Griffin [age] + 38) 3. Create an array using the Array_combine () function The Array_combine () function in PHP can combine two arrays into a new array, one of which is the key name and the value of the other array is the key value. Definition Format: Array array_combine (ARRAY1,ARRAY2)
- $a 1=array ("a", "B", "C", "D");
- $a 2=array ("Cat", "Dog", "Horse", "Cow");
- Print_r (Array_combine ($a 1, $a 2));
- ?>
Copy CodeOutput: Array ([a] = Cat = Dog [c] = Horse [d] = Cow) Note: When using the Array_combine () function, two parameters must have the same number of elements. 4. Create an array using the range () function Definition Format: Array range (FIRST,SECOND,STEP) First: element minimum second: element max step: Element step The official definition: The function creates an array that contains integers or characters from first to second (containing first and second). If the second is smaller than first, the inverted array is returned. Example 1:
- $number = range (0,5);
- Print_r ($number);
- ?>
Copy CodeOutput: Array ([0] = 0 [1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] = 5) Example 2:
- $number = range (0,50,10);
- Print_r ($number);
- ?>
Copy CodeOutput: Array ([0] = 0 [1] = [2] = [3] = [4] = [5] = 50) Example 3:
- $letter = Range ("A", "D");
- Print_r ($letter);
- ?>
Copy CodeOutput: Array ([0] = a [1] = b [2] = = c [3] + D) 5. Create an array using the Array_fill () function The Array_fill () function fills an array with the given value class Definition Format: Array_fill (start,number,value) Start: Start index number: Array count value: Array values Example:
- $a =array_fill (2,3, "Dog");
- Print_r ($a);
- ?>
Copy CodeOutput: Array ([2] = dog [3] = dog [4] = dog) |