5 PHP array creation instance code sharing

  • 2020-12-19 20:56:33
  • OfStack

Before reading this article, I believe that all of you have read the PHP Chinese manual about the section 1 on arrays. How about it? How much do you understand? At least the first time I read the document, I was confused. Maybe it was because the translation was not easy to understand. Here UncleToo, based on its own experience, shares the various ways of creating arrays with PHP instance code, hoping to help you (of course, the PHP documentation is still needed).

1. Create an array using array()

array() creating arrays is one of the most common ways we use PHP development, and array() is more of a structure than a function.

Example 1:
 
<?php 
$number = array(1,3,5,7,9); 
$color =array("red","blue","green"); 
$student = array("name",17) 
?> 

Example 2:
 
<?php 
$language = array(1=>"PHP",3=>"JAVA",4=>"C"); 
$student = array("name"=>" zhang 3","age"=>17) 
?> 

Of course, no value in an array is allowed, that is, an empty array:
 
<?php 
$result = array(); 
?> 

2. Create an array using the compact() function

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

Definition format:

array compact(var1,var2...)

Example 1: Any string that does not have a variable name corresponding to it is skipped.
 
<?php 
$firstname = "Peter"; 
$lastname = "Griffin"; 
$age = "38"; 
$result = compact("firstname", "lastname", "age"); 
print_r($result); 
?> 

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

Example 2: Use a string with no corresponding variable name and an array of 1 variable name
 
<?php 
$firstname = "Peter"; 
$lastname = "Griffin"; 
$age = "38"; 
$name = array("firstname", "lastname"); 
$result = compact($name, "location", "age"); 
print_r($result); 
?> 

Output results:
 
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 with a key name and one with a key value.

Definition format:

array array_combine(array1,array2)

The sample
 
<?php 
$a1=array("a","b","c","d"); 
$a2=array("Cat","Dog","Horse","Cow"); 
print_r(array_combine($a1,$a2)); 
?> 

Output results:

Array ( [a] = > Cat [b] = > Dog [c] = > Horse [d] = > Cow )

Note: When using the array_combine() function, the two arguments 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 maximum

step: Element step size

Here's the official definition: This function creates an array of integers or characters from first to second, including first and second. If second is smaller than first, an unordered array is returned.

It's hard to understand, so let's go straight to the examples (I like to see tutorials with examples).

Example 1:
 
<?php 
$number = range(0,5); 
print_r ($number); 
?> 

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

Example 2:
 
<?php 
$number = range(0,50,10); 
print_r ($number); 
?> 

Output results:
 
Array 
( 
[0] => 0 
[1] => 10 
[2] => 20 
[3] => 30 
[4] => 40 
[5] => 50 
) 

Example 3:
 
<?php 
$letter = range("a","d"); 
print_r ($letter); 
?> 

Output results:
 
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: Starting index

number: Number of arrays

value: Array values

Example:
 
<?php 
$a=array_fill(2,3,"Dog"); 
print_r($a); 
?> 

Output results:

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

Related articles: