PHP array learning

  • 2020-05-05 11:04:10
  • OfStack

Today we learned about arrays, which are arguably one of the more important ways to use PHP's data. There are many array functions of PHP. The following is a summary of my study.
1. Array definition:
The array is defined using array (), and you can define an empty array:
 
<?php 
$number = array(1,3,5,7,9); 
// Define an empty array  
$result = array(); 
$color =array("red","blue","green"); 
// Custom key values  
$language = (1=>"English",3=>"Chinese",5=>"Franch"); 
// Define a two-dimensional array  
$two = array( 
"color"=>array("red","blue"), // End with a comma  
"week"=>array("Monday","Friday") // There is no punctuation in the last sentence  
) ;  
?> 

2. Create an array:
The
creation array contains functions compact() and
1.compact() function -- converts one or more variables (containing arrays) to an array:
array compact (mixed $varname [, mixed $...])
 
<?PHP 
$number = "1,3,5,7,9"; 
$string = "I'm PHPer"; 
$array = array("And","You?"); 
$newArray = compact("number","string","array"); 
print_r ($newArray); 
?> 

The compact() function is used to convert two or more variables into an array, which also includes array variables. The argument is the name of the variable rather than the full name with $.
The opposite function is extract(), which converts an array to a single string with the key as its string name and the array value as its string value.
Results:
 
Array ( [number] => 1,3,5,7,9 [string] => I'm PHPer [array] => Array ( [0] => And [1] => You? ) ) 

2.array_combine() -- recombines two arrays into an array, one for keys and one for values:
array array_combine (array $keys, array $values)
 
<?PHP 
$number = array("1","3","5","7","9"); 
$array = array("I","Am","A","PHP","er"); 
$newArray = array_combine($number,$array); 
print_r ($newArray); 
?> 

array_combine function not to say, who read all understand
Results:

Array ( [1] = > I [3] = > Am [5] = > A [7] = > PHP [9] = > er )
3.range() function -- creates an array of specified ranges:
Without further elaboration, go straight to the instance --
 
<?PHP 
$array1 = range(0,100,10);//0 Is the initial value, 100 Is the end value, 10 For the step value ( The default step value is 1). 
print_r($array1); 
echo"<br />"; 
$array2 = range("A","Z"); 
print_r($array2); 
echo "<br />"; 
$array3 = range("z","a"); 
print_r($array3); 
?> 

The default step value of the range() function is 1!
Result:
 
Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 ) 
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z ) 
Array ( [0] => z [1] => y [2] => x [3] => w [4] => v [5] => u [6] => t [7] => s [8] => r [9] => q [10] => p [11] => o [12] => n [13] => m [14] => l [15] => k [16] => j [17] => i [18] => h [19] => g [20] => f [21] => e [22] => d [23] => c [24] => b [25] => a ) 

array_fill() function -- fill array function:
 
<?PHP 
$array = range(1,10); 
$fillarray = range("a","d"); 
$arrayFilled = array_fill(0,5,$fillarray);// Here, $fillarray It could be a string, like "test". 
echo "<pre>"; 
print_r ($arrayFilled); 
echo "</pre>"; 
$keys = array("string","2",9,"SDK","PK"); 
$array2 = array_fill_keys($keys,"testing"); 
echo "<pre>"; 
print_r ($array2); 
echo "</pre>"; 
?> 

Results:
 
Array 
( 
[0] => Array 
( 
[0] => a 
[1] => b 
[2] => c 
[3] => d 
) 
[1] => Array 
( 
[0] => a 
[1] => b 
[2] => c 
[3] => d 
) 
[2] => Array 
( 
[0] => a 
[1] => b 
[2] => c 
[3] => d 
) 
[3] => Array 
( 
[0] => a 
[1] => b 
[2] => c 
[3] => d 
) 
[4] => Array 
( 
[0] => a 
[1] => b 
[2] => c 
[3] => d 
) 
) 
Array 
( 
[string] => testing 
[2] => testing 
[9] => testing 
[SDK] => testing 
[PK] => testing 
) 

Array traversal:
1.foreach traversal:
foreach (array_expression as $value){}
foreach (array_expression as $key => $value){}

,
,
,
,
,
,
 
<?PHP 
$speed = array(50,120,180,240,380); 
foreach($speed as $keys=>$values){ 
echo $keys."=>".$values."<br />"; 
} 
?> 

Result:
 
0=>50 
1=>120 
2=>180 
3=>240 
4=>380 

2.while loop traversal:
while loop traversal is generally combined with list functions. Here is an example of
 
<?PHP 
$staff = array( 
array(" The name "," gender "," age "), 
array(" Xiao zhang, "," male ",24), 
array(" wang "," female ",25), 
array(" Xiao li "," male ",23) 
); 
echo "<table border=2>"; 
while(list($keys,$value) = each($staff)){ 
list($name,$sex,$age) = $value; 
echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>"; 
} 
echo "</table>"; 
?> 

Result:
Name gender age xiaozhang male 24 Wang female 25 Xiao li male 233.for circular traversal:
 
<?PHP 
$speed = range(0,220,20); 
for($i =0;$i<count($speed);$i++) { 
echo $speed[$i]." "; 
} 
?> 

Result:
 
0 20 40 60 80 100 120 140 160 180 200 220 

Related articles: