5 PHP instances for creating arrays to share code

Source: Internet
Author: User
In this article, we will share with you various methods of creating arrays using PHP instance code, if you are interested, you can understand this article. before reading this article, I believe everyone has read the PHP Chinese manual's explanation of the array section. how can I understand it? At least I was confused when I first read the document. maybe it is because the translation is not easy to understand. ^_^ !! Here, based on your own experience, UncleToo will share various array creation methods with you through the PHP instance code method, hoping to help you (of course, you should read more about the PHP document)

1. use array () to create an array

Array () is the most common method in PHP Development. to be precise, array () is a structure rather than a function.

Example 1:
The code is as follows:
$ Number = array (1, 3, 5, 7, 9 );
$ Color = array ("red", "blue", "green ");
$ Student = array ("name", 17)
?>

Example 2:
The code is as follows:
$ Language = array (1 => "PHP", 3 => "JAVA", 4 => "C ");
$ Student = array ("name" => "James", "age" => 17)
?>

Of course, no value in the array is allowed, that is, an empty array:
The code is as follows:
$ Result = array ();
?>

2. use the compact () function to create an array

The compact () function in PHP can convert one or more variables into arrays.

Definition format:

Array compact (var1, var2 ...)

Example 1: Any string without the corresponding variable name is skipped.
The code is as follows:
$ Firstname = "Peter ";
$ Lastname = "Griffin in ";
$ Age = "38 ";
$ Result = compact ("firstname", "lastname", "age ");
Print_r ($ result );
?>

Output result:
The code is as follows:
Array
(
[Firstname] => Peter
[Lastname] => Griffin in
[Age] => 38
)

Example 2: Use a string without a corresponding variable name and an array of variable names
The code is as follows:
$ Firstname = "Peter ";
$ Lastname = "Griffin in ";
$ Age = "38 ";
$ Name = array ("firstname", "lastname ");
$ Result = compact ($ name, "location", "age ");
Print_r ($ result );
?>

Output result:
The code is as follows:
Array
(
[Firstname] => Peter
[Lastname] => Griffin in
[Age] => 38
)

3. use the array_combine () function to create an array

In PHP, the array_combine () function can combine two arrays into a new array. one of the arrays is the key name, and the other array is the key value.

Definition format:

Array array_combine (array1, array2)

Example
The code is as follows:
$ A1 = array ("a", "B", "c", "d ");
$ A2 = array ("Cat", "Dog", "Horse", "Cow ");
Print_r (array_combine ($ a1, $ a2 ));
?>

Output result:

Array ([a] => Cat [B] => 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

Definition format:

Array range (first, second, step)

First: minimum element value

Second: maximum element value

Step: element step size

The following is an 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, an array in reverse order is returned.

It is hard to understand. let's look at the example directly (I like the example tutorial ).

Example 1:
The code is as follows:
$ Number = range (0, 5 );
Print_r ($ number );
?>

Output result:
The code is as follows:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)

Example 2:
The code is as follows:
$ Number = range (0, 50, 10 );
Print_r ($ number );
?>

Output result:
The code is as follows:
Array
(
[0] => 0
[1] => 10
[2] => 20
[3] => 30
[4] => 40
[5] => 50
)

Example 3:
The code is as follows:
$ Letter = range ("a", "d ");
Print_r ($ letter );
?>

Output result:
The code is as follows:
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
)

5. use the array_fill () function to create an array

The array_fill () function fills an array with a given value class.

Definition format:

Array_fill (start, number, value)

Start: start Index

Number: number of arrays

Value: Array value

Example:
The code is as follows:
$ A = array_fill (2, 3, "Dog ");
Print_r ($ );
?>

Output result:

Array ([2] => Dog [3] => Dog [4] => Dog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.