Chapter 5 php array operations

  • 2020-05-10 17:51:23
  • OfStack

1. What is an array
An array is a group of elements that have certain characteristics in common, including similarity and type.
Each element is distinguished by a special identifier, called key, and each key has an value
1. Two ways to create an array:
1.1 use the array() function
 
<?php 
$usernames = array ('Alerk', 'Mary', 'Lucy', 'Bob', 'Jack', 'John', 'Mark' ); 
foreach ( $usernames as $name ) 
{ 
echo $name . '<br/>'; 
} 
?> 

output
Alerk
Mary
Lucy
Bob
Jack
John
Mark
1.2 use the range() function
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 

output
0
1
2
3
4
5
6
7
8
9
10
a

c
d
e
f
g
h
i
j
k
l
m

o

q
r

t
u
v
w
x
y
z
2. Loop access to array elements in two ways:
2.1 for cycle
 
<?php 
//range The first 3 Two parameters represent the step size  
$numbers = range(1,10,2); 
for($i = 0;$i<count($numbers); $i ++) 
{ 
echo $numbers[$i].'<br/>'; 
} 
?> 

output
1
3
5
7
9
2.2 foreach cycle
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 

output
a
c
e
g
Foreach can also be used to output array subscripts and corresponding values
 
<?php 
$letters = range('a','g',2); 
foreach($letters as $key => $value) 
{ 
echo $key.'---'.$value.'<br/>'; 
} 
?> 

output
0---a
1---c
2---e
3---g
3.is_array() function, used to determine whether the variable is an array
 
<?php 
$numbers = range(1,10,2); 
if(is_array($numbers)) 
{ 
foreach($numbers as $num) 
{ 
echo $num.'<br/>'; 
} 
} 
else 
{ 
echo $numbers; 
} 
?> 

4. The print_r function prints easy-to-understand information about variables
 
<?php 
$usernames = array ('Jackie', 'Mary', 'Lucy', 'Bob', 'Mark', 'John' ); 
print_r ( $usernames ); 
?> 

output
Array ( [0] = > Jackie [1] = > Mary [2] = > Lucy [3] = > Bob [4] = > Mark [5] = > John )
The source code can be seen as:
Array
(
[0] = > Jackie
[1] = > Mary
[2] = > Lucy
[3] = > Bob
[4] = > Mark
[5] = > John
)
2. Custom key array
1. If you don't want to create an array with a default index of zero, you can create an array with the keys as strings using the following method
 
<?php 
// Initialize array  
$userages = array('Jack'=> 23,'Lucy'=>25,'Mark'=>28); 
// Access the elements of the array  
echo $userages['Jack'].'<br/>'; 
echo $userages['Lucy'].'<br/>'; 
echo $userages['Mark'].'<br/>'; 
?> 

2. Append elements to a custom key array
 
<?php 
// Initialize array  
$ages = array('Jack'=>23); 
// Additional elements  
$ages['Lucy']=25; 
$ages['Mark']=28; 
foreach($ages as $key => $value) 
{ 
echo $key.'----'.$value.'<br/>'; 
} 
?> 

3. Add elements directly without creating an array.
 
<?php 
// Add without creating an array  
$ages['Jack']=23; 
$ages['Lucy']=25; 
$ages['Mark']=28; 
foreach($ages as $key => $value) 
{ 
echo $key.'----'.$value.'<br/>'; 
} 
?> 

4. Use of loop printing array foreach
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
0
5. each() -- returns the current key/value pair in the array and moves the array pointer 1 step forward
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
1
Use the each() function for circular printing
 
<?php 
$ages['Jack']=23; 
$ages['Lucy']=25; 
$ages['Mark']=28; 
while(!! $element = each($ages)) 
{ 
print_r($element); 
echo '<br>'; 
} 
?> 

Another way to print
 
<?php 
$ages['Jack']=23; 
$ages['Lucy']=25; 
$ages['Mark']=28; 
while(!! $element = each($ages)) 
{ 
echo $element['key'].'=>'.$element['value']; 
echo '<br>'; 
} 
?> 

6. Use of the list() function -- assign values in an array to 1 variable
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
4
Print the result in list loop
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
5
output
Jack= > 23
Lucy= > 25
Mark= > 28
7. Use of the reset() function -- points the internal pointer to the first cell of the array
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
6
Output
Mark= > 28
Jack= > 23
8. array_unique() -- removes duplicate values from an array
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
7
output
Array ( [23] = > Jack [25] = > Lucy [28] = > Mark )
3. An array of arrays
An array is a list of a key and a value. You can also put an array in an array
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
8
output
apple|6|28.8
pear|3|15.6
banana|10|4.6
Print the array in the array with for loop
 
<?php 
$numbers = range ( 0, 10 ); 
foreach ( $numbers as $num ) 
{ 
echo $num . '<br/>'; 
} 
$letters = range ( 'a', 'z' ); 
foreach ( $letters as $letter ) 
{ 
echo $letter . '<br/>'; 
} 
?> 
9
output
|apple|6|28.8
|pear|3|15.6
|banana|10|4.6
2 d array
 
<?php 
//range The first 3 Two parameters represent the step size  
$numbers = range(1,10,2); 
for($i = 0;$i<count($numbers); $i ++) 
{ 
echo $numbers[$i].'<br/>'; 
} 
?> 
0
output
|name= > apple|amount= > 6|price= > 28.8
|name= > pear|amount= > 3|price= > 15.6
|name= > banana|amount= > 10|price= > 4.6
Printing with foreach is easier (recommended)
 
<?php 
$produces = array ( 
array ('name' => 'apple', 'amount' => 6, 'price' => 28.8 ), 
array ('name' => 'pear', 'amount' => 3, 'price' => 15.6 ), 
array ('name' => 'banana', 'amount' => 10, 'price' => 4.6 ) 
); 
foreach($produces as $key1 => $value1) 
{ 
foreach($value1 as $key2 => $value2) 
{ 
echo '|'.$key2.'=>'.$value2; 
} 
echo '<br>'; 
} 
?> 

output
|name= > apple|amount= > 6|price= > 28.8
|name= > pear|amount= > 3|price= > 15.6
|name= > banana|amount= > 10|price= > 4.6
4. Sort of array
1.Sort() function to sort English
 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
<?php 
$fruits = array('lemo','banana','apple','pear'); 
echo ' Original array: '; 
print_r($fruits); 
echo '<br/>'; 
sort($fruits); 
echo ' Sorted array: '; 
print_r($fruits); 
?> 

output
Original array: Array ([0] = > lemo [1] = > banana [2] = > apple [3] = > pear )
Sorted array: Array ([0] = > apple [1] = > banana [2] = > lemo [3] = > pear )
2. Ordering of Chinese by Sort() function
 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
<?php 
$fruits = array(' lemon ',' banana ',' apple ',' pears '); 
echo ' Original array: '; 
print_r($fruits); 
echo '<br/>'; 
sort($fruits); 
echo ' Sorted array: '; 
print_r($fruits); 
?> 

Output:
The original array: Array ([0] = > Lemon [1] = > Banana [2] = > Apple [3] = > Pears)
Sorted array: Array ([0] = > Lemon [1] = > Pear [2] = > Apple [3] = > Banana)
3. asort -- sorts the array and maintains the index
 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
<?php 
$fruits = array('a'=>' lemon ','b'=>' banana ','c'=>' apple ','d'=>' pears '); 
echo ' Original array: '; 
print_r($fruits); 
echo '<br/>'; 
asort($fruits); 
echo ' Sorted array: '; 
print_r($fruits); 
?> 

output
Original array: Array ([a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
Sorted array: Array ([a] = > Lemon [d] = > Pears = [c] > Apple [b] = > Banana)
4. ksort -- sorts the array by key name
 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
<?php 
$fruits = array('b'=>' lemon ','a'=>' banana ','d'=>' apple ','c'=>' pears '); 
echo ' Original array: '; 
print_r($fruits); 
echo '<br/>'; 
ksort($fruits); 
echo ' Sorted array: '; 
print_r($fruits); 
?> 

output
The original array: Array ([b] = > Lemon [a] = > Banana [d] = > Apple [c] = > Pears)
Sorted array: Array ([a] = > Banana [b] = > Lemon [c] => Pears = [d] > Apple)
5. rsort -- reverse sort the array
 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
<?php 
$fruits = array(' lemon ',' banana ',' apple ',' pears '); 
echo ' Original array: '; 
print_r($fruits); 
echo '<br/>'; 
rsort($fruits); 
echo ' Sorted array: '; 
print_r($fruits); 
?> 

output
The original array: Array ([0] = > Lemon [1] = > Banana [2] = > Apple [3] = > Pears)
Sorted array: Array ([0] = > Banana [1] = > Apple [2] = > Pear [3] = > Lemon)
6. arsort -- reverse sort the array and keep the index
 
<?php 
//range The first 3 Two parameters represent the step size  
$numbers = range(1,10,2); 
for($i = 0;$i<count($numbers); $i ++) 
{ 
echo $numbers[$i].'<br/>'; 
} 
?> 
7
output
Original array: Array ([a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
Sorted array: Array ([b] = > Banana [c] = > Apple [d] = > Pears = [a] > Lemon)
7. krsort -- reverse sort the array by key name
 
<?php 
//range The first 3 Two parameters represent the step size  
$numbers = range(1,10,2); 
for($i = 0;$i<count($numbers); $i ++) 
{ 
echo $numbers[$i].'<br/>'; 
} 
?> 
8
output
Original array: Array ([a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
Sorted array: Array ([d] = > Pears = [c] > Apple [b] = > Banana [a] = > Lemon)
8. shuffle -- shuffles the array
 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
<?php 
$fruits = array('a'=>' lemon ','b'=>' banana ','c'=>' apple ','d'=>' pears '); 
echo ' Original array: '; 
print_r($fruits); 
echo '<br/>'; 
shuffle($fruits); 
echo ' Scrambled array: '; 
print_r($fruits); 
?> 

output
Original array: Array ([a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
Scrambled array: Array ([0] = > Banana [1] = > Apple [2] = > Lemon [3] = > Pears)
9. array_reverse -- returns an array of cells in reverse order
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
0
output
Original array: Array ([a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
Reversed array: Array ([d] = > Pears = [c] > Apple [b] = > Banana [a] = > Lemon)
10. array_unshift -- insert one or more cells at the beginning of the array
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
1
output
Original array: Array ([a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
The inserted array: Array ([0] = > � [a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
11. array_shift -- moves the element at the beginning of the array out of the array
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
2
output
Original array: Array ([a] = > Lemon [b] = > Banana [c] = > Apple [d] = > Pears)
The removed array: Array ([b] = > Banana [c] = > Apple [d] = > Pears)
12. array_rand -- randomly extract one or more cells from the array
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
3
output
Original array: Array ([0] = > Lemon [1] = > Banana [2] = > Apple [3] = > Pears)
Random array: pears and apples
13. array_pop -- eject the last cell of the array
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
4
Output:
Original array: Array ([0] = > Lemon [1] = > Banana [2] = > Apple [3] = > Pears)
Popup array: Array ([0] = > Lemon [1] = > Banana [2] = > Apple)
14. array_push -- push 1 or more cells into the end of the array (push)
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
5
Output:
Original array: Array ([0] = > Lemon [1] = > Banana [2] = > Apple [3] = > Pears)
Popup array: Array ([0] = > Lemon [1] = > Banana [2] = > Apple [3] = > Pear [4] = > �)
5. The operation of a pointer to an array
each -- returns the current key/value pair in the array and moves the array pointer 1 step forward
current -- returns the current cell in the array
reset -- points the internal pointer to the first cell of the array
end -- points the internal pointer to the last cell of the array
next -- moves the internal pointer in the array one bit forward
pos -- alias for current()
prev - rewinds the internal pointer to the array back 1 bit
< meta http-equiv="content-type" content="text/html;charset=utf-8" / >
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
6
Output:
Array ( [0] = > Lemon [1] = > Banana [2] = > Apple [3] = > Pears)
each() : Array ( [1] = > Lemon [value] = > Lemon [0] = > 0 [key] = > 0 )
current () : a banana
next () : the apple
end () : pears
prev () : the apple
pos () : the apple
6. Count the number of arrays
count -- counts the number of cells in an array or the number of attributes in an object
sizeof -- alias for count()
array_count_values -- counts the number of times all the values in the array appear
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
7
output
22
22
Array ( [1] = > 6 [3] = > 2 [5] = > 4 [4] = > 7 [65] = > 1 [2] = > 2 )
7. Convert array to scalar variable: extract()
Each element in the array is converted into a variable with the name key of the array element and the value value of the array element.
 
<?php 
$letters = range('a','h',2); 
foreach($letters as $letter) 
{ 
echo $letter.'<br/>'; 
} 
?> 
8
output
apple
banana
orange

Related articles: