Home > Backend Development > PHP Tutorial > 5 ways to create arrays in PHP

5 ways to create arrays in PHP

WBOY
Release: 2016-07-25 08:54:58
Original
1373 people have browsed it
  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 also allowed, that is, an empty array:

  1. $result = array();
  2. ?>
Copy code

2. Use the compact() function to create an array The compact() function in PHP can convert one or more variables into an array

Define format: array compact(var1,var2...)

Any string that does not have a corresponding variable name is ignored.

  1. $firstname = "Peter";
  2. $lastname = "Griffin";
  3. $age = "38";
  4. $result = compact("firstname", "lastname", "age ");
  5. print_r($result);
  6. ?>
Copy the code

Output result: Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 )

Use a string without a corresponding variable name, and an array of variable names

  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 result: Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 )

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

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

Define format: array array_combine(array1,array2)

  1. $a1=array("a","b","c","d");
  2. $a2=array("Cat","Dog","Horse" ,"Cow");
  3. print_r(array_combine($a1,$a2));
  4. ?>
Copy code

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

Define format: array range(first,second,step) first: minimum element value second: the maximum value of the element step: element step size

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, return the array in reverse order.

Example 1:

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

Output result: 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 result: Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )

Example 3:

  1. $letter = range("a","d");
  2. print_r ($letter);
  3. ?>
Copy code

Output result: Array ( [0] => a [1] => b [2] => c [3] => d )

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

The

array_fill() function fills the array with a given value class

Define format: array_fill(start,number,value) start: starting index number: number of arrays value: array value

Example:

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

Output result: Array ( [2] => Dog [3] => Dog [4] => Dog )



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template