In this article, the various ways of creating arrays are shared with PHP instance code, and interested friends can understand the
Read this article before I believe that everyone has read the PHP Chinese Manual on the section of the interpretation of the array, how do you know how much? At least the first time I read the document is confused, perhaps because the translation is not easy to understand it ^_^!! Here Uncletoo based on their own experience, the array of various ways to create the form of PHP instance code to share to everyone, I hope to have some help (of course, PHP documents are still more to see) 1, use Array () to create an array Array () creating arrays is one of the most common ways we use in the PHP development process, and an array () is a structure rather than a function. Example 1: code is as follows: <?php $number = Array (1,3,5,7,9); $color =array ("Red", "Blue", "green"); nbsp $student = Array ("name" ?> example 2: code as follows: <?php $language = Array (1=> "PHP" ,3=> "JAVA",4=> "C"); $student = Array ("name" => "John", "Age" =>17) ?> of course, There is no value in the array is allowed, that is, empty array: Copy code code is as follows: <?php $result = Array (); ?> 2, creating arrays using the compact () function the Compact () function in PHP can convert one or more variables to an array definition Format: Array compact (VAR1,VAR2 ...) Example 1: Any string that has no variable name corresponding to it is skipped. Code as follows: <?php $firstname = "Peter"; $lastname = "Griffin"; $age = "$result"; = Co Mpact ("FirstName", "LastName", "Age"); Print_r ($result); ?> output: code is as follows: array ( [FirstName] = > peter [LastName] => griffin [age] => 38 ) Example 2: Use a string with no corresponding variable name, and a variable array group code such as Under: <?php $firstname = "Peter"; $lastname = "Griffin"; $age = "; $name = Array (" FirstName "," LastName "); $result = Compact ($name," location "," age "); Print_r ($result); ?> Output results: & nbsp The code is as follows: array ( [FirstName] => peter [LastName] => griffin [age] => 38 ) 3, using the Array_combine () function to create an array PHP array_combine () function can combine two arrays into a new array, one of which is the key name and the other is the key value. Definition Format: array array_combine (ARRAY1,ARRAY2) example code is as follows: <?php $a 1=array ("A", "B", "C", "D"); $a 2=array ("Cat", "Dog", "Horse", "Cow"); Print_r (Array_combine ($a 1, $a 2)); ?> Output: Array ([a] => Cat [b] => Dog [C] => horse [d] => Cow) NOTE: When using the Array_combine () function, two parameters must have the same number of elements. 4, using the range () function to create an array definition Format: array range (First,second,step) : element min Second: element max Step: Element Step Below is the official definition: The function creates an array containing the values from the beginning to the second (including the One and second) An integer or character between. If the second is smaller than the top, an array of reversed sequences is returned. It's hard to understand, so let's take a look at the examples (I like to see examples of tutorials). Example 1: code is as follows: <?php $number = Range (0,5); print_r ($number); ?> Output results : code is as follows: array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4  ; [5] => 5 Example 2: code is as follows: <?php $number = Range (0,50,10); print_r ($number); ?> Output: code is as follows: array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 ) example 3: code as follows: <?php $letter = Range ("A", "D"); Print_r ($letter); ?> output: code is as follows: array ( [0] => a [1] => b [2] => c [3] => d ) 5, creating an array using the Array_fill () function   ; The Array_fill () function fills the array definition format with the given value class: Array_fill (start,number,value) Start: Start index Number: Array count value: Array value Example: code is as follows: <?php $a =array_fill ( 2,3, "Dog"); Print_r ($a); ?> output: Array ([2] => Dog [3] => Dog [4] => D OG)