5 Examples of PHP-created arrays for instance code sharing _php Tutorial

Source: Internet
Author: User
Read this article before I believe that everyone has read the PHP manual on the section of the explanation, how to understand how much? At least the first time I read the document is confused, perhaps because the translation is not easy to understand ^_^!! Here Uncletoo in accordance with their own experience, the array of various ways to create the PHP instance code to share the way to everyone, I hope to have some help (of course, PHP documents to see more)

1. Use array () to create arrays

Array () creating arrays is one of the most common ways we use PHP in the development process, and it is accurate to say that array () is a struct rather than a function.

Example 1:
Copy CodeThe code is as follows:
$number = Array (1,3,5,7,9);
$color =array ("Red", "Blue", "green");
$student = Array ("name", 17)
?>

Example 2:
Copy CodeThe code is as follows:
$language = Array (1=> "PHP",3=> "JAVA",4=> "C");
$student = Array ("name" = "Zhang San", "Age" =>17)
?>

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

2. Create an array using the compact () function

The compact () function in PHP can convert one or more variables into an array

Define the format:

Array Compact (VAR1,VAR2 ...)

Example 1: Any string that has no variable name corresponding to it is skipped.
Copy CodeThe code is as follows:
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = Compact ("FirstName", "LastName", "age");
Print_r ($result);
?>

Output Result:
Copy CodeThe code is as follows:
Array
(
[FirstName] = Peter
[LastName] = Griffin
[Age] = 38
)

Example 2: Using a string with no corresponding variable name, and a variable an array group
Copy CodeThe code is as follows:
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$name = Array ("FirstName", "LastName");
$result = Compact ($name, "location", "age");
Print_r ($result);
?>

Output Result:
Copy CodeThe code is 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 value of the other array is the key value.

Define the format:

Array Array_combine (ARRAY1,ARRAY2)

Example
Copy CodeThe code is as follows:
$a 1=array ("a", "B", "C", "D");
$a 2=array ("Cat", "Dog", "Horse", "Cow");
Print_r (Array_combine ($a 1, $a 2));
?>

Output Result:

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)

First: element minimum value

Second: element maximum value

Step: Element Step

Here is 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.

It is difficult to understand, we look directly at examples (I like to see examples of the tutorial).

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

Output Result:
Copy CodeThe code is as follows:
Array
(
[0] = 0
[1] = 1
[2] = 2
[3] = 3
[4] = 4
[5] = 5
)

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

Output Result:
Copy CodeThe code is as follows:
Array
(
[0] = 0
[1] = 10
[2] = 20
[3] = 30
[4] = 40
[5] = 50
)

Example 3:
Copy CodeThe code is as follows:
$letter = Range ("A", "D");
Print_r ($letter);
?>

Output Result:
Copy CodeThe code is as follows:
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

Define the format:

Array_fill (Start,number,value)

Start: Start index

Number: Array

Value: Array value

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

Output Result:

Array ([2] = dog [3] = dog [4] = dog)

http://www.bkjia.com/PHPjc/710594.html www.bkjia.com true http://www.bkjia.com/PHPjc/710594.html techarticle read this article before I believe that everyone has read the PHP manual on the section of the explanation, how to understand how much? At least I was confused the first time I read a document ...

  • 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.