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 still need to see more)
1. Create an array using 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:
Copy Code code as follows:
<?php
$number = Array (1,3,5,7,9);
$color =array ("Red", "Blue", "green");
$student = Array ("name", 17)
?>
Example 2:
Copy Code code as follows:
<?php
$language = Array (1=> "PHP",3=> "JAVA",4=> "C");
$student = Array ("name" => "John", "Age" =>17)
?>
Of course, there are no values in the array that are allowed, that is, empty arrays:
Copy Code code as follows:
<?php
$result = Array ();
?>
2. Create an array using the compact () function
The compact () function in PHP converts one or more variables to an array
Define the format:
Array Compact (VAR1,VAR2 ...)
Example 1: Any string with no variable name corresponding to it is skipped.
Copy Code code as follows:
<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = Compact ("FirstName", "LastName", "age");
Print_r ($result);
?>
Output results:
Copy Code code as follows:
Array
(
[FirstName] => Peter
[LastName] => Griffin
[Age] => 38
)
Example 2: Using a string with no corresponding variable name, and a variable array group
Copy Code code as follows:
<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$name = Array ("FirstName", "LastName");
$result = Compact ($name, "location", "age");
Print_r ($result);
?>
Output results:
Copy Code code as follows:
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 other is the key value.
Define the format:
Array Array_combine (ARRAY1,ARRAY2)
Example
Copy Code code 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 results:
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. Create an array using the range () function
Define the format:
Array range (FIRST,SECOND,STEP)
The minimum value of the element:
Second: element maximum value
Step: Element Steps
Here is the official definition: The function creates an array that contains integers or characters from the beginning to the second (including the one and the second). If the second is smaller than the top, an array of reversed sequences is returned.
It is difficult to understand, we directly look at the example (I like to see examples of tutorials).
Example 1:
Copy Code code as follows:
<?php
$number = range (0,5);
Print_r ($number);
?>
Output results:
Copy Code code as follows:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
Example 2:
Copy Code code as follows:
<?php
$number = range (0,50,10);
Print_r ($number);
?>
Output results:
Copy Code code as follows:
Array
(
[0] => 0
[1] => 10
[2] => 20
[3] => 30
[4] => 40
[5] => 50
)
Example 3:
Copy Code code as follows:
<?php
$letter = Range ("A", "D");
Print_r ($letter);
?>
Output results:
Copy Code code as follows:
Array
(
[0] => a
[1] => b
[2] => C
[3] => D
)
5. Create an array using the Array_fill () function
The Array_fill () function populates an array with the given value class
Define the format:
Array_fill (Start,number,value)
Start: Start index
Number: Array Count
Value: Array values
Example:
Copy Code code as follows:
<?php
$a =array_fill (2,3, "Dog");
Print_r ($a);
?>
Output results:
Array ([2] => Dog [3] => Dog [4] => Dog)