Example 2:
Of course, no value in the array is also allowed, that is, an empty array:
2. Use the compact() function to create an array The compact() function in PHP can convert one or more variables into an array Define format: array compact(var1,var2...) Any string that does not have a corresponding variable name is ignored.
Output result: Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 ) Use a string without a corresponding variable name, and an array of variable names
Output result: Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 ) 3. Use the array_combine() function to create an array The array_combine() function in PHP can combine two arrays into a new array, where one array is the key name and the value of the other array is the key value. Define format: array array_combine(array1,array2)
Output result: Array ( [a] => Cat => Dog [c] => Horse [d] => Cow ) Note: When using the array_combine() function, the two parameters must have the same number of elements. 4. Use the range() function to create an array Define format: array range(first,second,step) first: minimum element value second: the maximum value of the element step: element step size Official definition: This function creates an array containing integers or characters from first to second (including first and second). If second is smaller than first, return the array in reverse order. Example 1:
Output result: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) Example 2:
Output result: Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 ) Example 3:
Output result: Array ( [0] => a [1] => b [2] => c [3] => d ) 5. Use the array_fill() function to create an array Thearray_fill() function fills the array with a given value class Define format: array_fill(start,number,value) start: starting index number: number of arrays value: array value Example:
Output result: Array ( [2] => Dog [3] => Dog [4] => Dog ) |