5 ways to create arrays in PHP

Source: Internet
Author: User
Tags compact
    1. $number = Array (1,3,5,7,9);
    2. $color =array ("Red", "Blue", "green");
    3. $student = Array ("name", 17)
    4. ?>
Copy Code

Example 2:

    1. $language = Array (1=> "PHP",3=> "JAVA",4=> "C");
    2. $student = Array ("name" = "Zhang San", "Age" =>17)
    3. ?>
Copy Code

Of course, no value in the array is allowed, that is, an empty array:

    1. $result = Array ();
    2. ?>
Copy Code

2. Using the Compact () function to create an array the compact () function in PHP can convert one or more variables to arrays

Definition Format: Array compact (VAR1,VAR2 ...)

Any string that has no variable name corresponding to it is skipped.

    1. $firstname = "Peter";
    2. $lastname = "Griffin";
    3. $age = "38";
    4. $result = Compact ("FirstName", "LastName", "age");
    5. Print_r ($result);
    6. ?>
Copy Code

Output: Array ([FirstName] = Peter [LastName] = Griffin [age] + 38)

Use a string with no corresponding variable name, and a variable an array group

    1. $firstname = "Peter";
    2. $lastname = "Griffin";
    3. $age = "38";
    4. $name = Array ("FirstName", "LastName");
    5. $result = Compact ($name, "location", "age");
    6. Print_r ($result);
    7. ?>
Copy Code

Output: 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.

Definition Format: Array array_combine (ARRAY1,ARRAY2)

    1. $a 1=array ("a", "B", "C", "D");
    2. $a 2=array ("Cat", "Dog", "Horse", "Cow");
    3. Print_r (Array_combine ($a 1, $a 2));
    4. ?>
Copy Code

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

Definition Format: Array range (FIRST,SECOND,STEP) First: element minimum second: element max step: Element step

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.

Example 1:

    1. $number = range (0,5);
    2. Print_r ($number);
    3. ?>
Copy Code

Output: Array ([0] = 0 [1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] = 5)

Example 2:

    1. $number = range (0,50,10);
    2. Print_r ($number);
    3. ?>
Copy Code

Output: Array ([0] = 0 [1] = [2] = [3] = [4] = [5] = 50)

Example 3:

    1. $letter = Range ("A", "D");
    2. Print_r ($letter);
    3. ?>
Copy Code

Output: 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

Definition Format: Array_fill (start,number,value) Start: Start index number: Array count value: Array values

Example:

    1. $a =array_fill (2,3, "Dog");
    2. Print_r ($a);
    3. ?>
Copy Code

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

  • Related Article

    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.