Fifth PHP array Operation _php Tutorial

Source: Internet
Author: User
Tags pears shuffle
A What is an array
An array is a set of elements that have some common characteristics, including similarity and type.
Each element is distinguished by a special identifier, called key, and each key has a value
1. Two ways to create an array:
1.1 using the array () function
Copy CodeThe code is as follows:
$usernames = Array (' Alerk ', ' Mary ', ' Lucy ', ' Bob ', ' Jack ', ' John ', ' Mark ');
foreach ($usernames as $name)
{
Echo $name. '
';
}
?>

Output
Alerk
Mary
Lucy
Bob
Jack
John
Mark
1.2 Using the range () function
Copy CodeThe code is as follows:
$numbers = Range (0, 10);
foreach ($numbers as $num)
{
Echo $num. '
';
}
$letters = Range (' A ', ' Z ');
foreach ($letters as $letter)
{
Echo $letter. '
';
}
?>

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. Iterate through the array elements in two ways:
2.1 For Loop
Copy CodeThe code is as follows:
The third parameter of range indicates the step size
$numbers = range (1,10,2);
for ($i = 0; $i {
echo $numbers [$i]. '
';
}
?>

Output
1
3
5
7
9
2.2 Foreach Loop
Copy CodeThe code is as follows:
$letters = Range (' A ', ' H ', 2);
foreach ($letters as $letter)
{
echo $letter. '
';
}
?>

Output
A
C
E
G
foreach can also be used to output the subscript and corresponding values of an array
Copy CodeThe code is as follows:
$letters = Range (' A ', ' G ', 2);
foreach ($letters as $key = $value)
{
echo $key. '---'. $value. '
';
}
?>

Output
0---A
1---C
2---E
3---G
3.is_array () function, used to determine whether an array is a variable
Copy CodeThe code is as follows:
$numbers = range (1,10,2);
if (Is_array ($numbers))
{
foreach ($numbers as $num)
{
echo $num. '
';
}
}
Else
{
Echo $numbers;
}
?>

4.print_r function to print easy-to-understand information about variables
Copy CodeThe code is as follows:
$usernames = Array (' Jackie ', ' Mary ', ' Lucy ', ' Bob ', ' Mark ', ' John ');
Print_r ($usernames);
?>

Output
Array ([0] = Jackie [1] = Mary [2] = Lucy [3] = Bob [4] = [5] = John)
In the source code you can see the display as:
Array
(
[0] = Jackie
[1] = Mary
[2] = Lucy
[3] = Bob
[4] = Mark
[5] = John
)
Two Custom Key Array
1. If you do not want to create an array with the default subscript zero, you can create an array of key strings in the following way
Copy CodeThe code is as follows:
Initializing an array
$userages = Array (' Jack ' = +, ' Lucy ' =>25, ' Mark ' =>28);
Accessing the elements of an array
echo $userages [' Jack ']. '
';
echo $userages [' Lucy ']. '
';
echo $userages [' Mark ']. '
';
?>

2. Append elements to the custom key array
Copy CodeThe code is as follows:
Initializing an array
$ages = Array (' Jack ' =>23);
Append element
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
foreach ($ages as $key = $value)
{
echo $key. '----'. $value. '
';
}
?>

3. Add elements directly without creating an array.
Copy CodeThe code is as follows:
Do not create arrays to add directly
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
foreach ($ages as $key = $value)
{
echo $key. '----'. $value. '
';
}
?>

4. Use of cyclic print set foreach
Copy CodeThe code is as follows:
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
foreach ($ages as $key = $value)
{
echo $key. ' = '. $value. '
';
}
?>

5. Each ()--Returns the current key/value pair in the array and moves the array pointer one step forward
Copy CodeThe code is as follows:
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
$a = each ($ages);
Print_r ($a);
Echo '
';
$a = each ($ages);
Print_r ($a);
Echo '
';
$a = each ($ages);
Print_r ($a);
?>

Use the each () function to cycle through the print
Copy CodeThe code is as follows:
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
while (!! $element = each ($ages))
{
Print_r ($element);
Echo '
';
}
?>

Another way to print
Copy CodeThe code is as follows:
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
while (!! $element = each ($ages))
{
echo $element [' key ']. ' = '. $element [' value '];
Echo '
';
}
?>

Use of the 6.list () function--assigns the values in the array to some variables
Copy CodeThe code is as follows:
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
List ($name, $age) = each ($ages);
echo $name. ' = '. $age;
?>

To cycle through the results with a list
Copy CodeThe code is as follows:
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
while (!! List ($name, $age) = each ($ages))
{
echo $name. ' = '. $age. '
';
}
?>

Output
Jack=>23
Lucy=>25
Mark=>28
Use of the 7.reset () function--point the inner pointer of the array to the first cell
Copy CodeThe code is as follows:
$ages [' Jack ']=23;
$ages [' Lucy ']=25;
$ages [' Mark ']=28;
each ($ages);
each ($ages);
List ($name, $age) = each ($ages);
echo $name. ' = '. $age. '
';
Set the array back to the beginning of the array
Reset ($ages);
List ($name, $age) = each ($ages);
echo $name. ' = '. $age. '
';
?>

Output
Mark=>28
Jack=>23
8. Array_unique ()--Remove duplicate values from the divisor group
Copy CodeThe code is as follows:
$nums = Array (1,2,3,4,5,6,5,4,3,2,1,1,2,3,4,5,6);
Returns an array that does not contain duplicate values
$result = Array_unique ($nums);
Print_r ($result);
?>
Output
Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6)
9. Array_flip ()--swap keys and values in an array
$userages = Array (' Jack ' = +, ' Lucy ' =>25, ' Mark ' =>28);
$ages = Array_flip ($userages);
Print_r ($ages);
?>

Output
Array ([+] = Jack [+] = Lucy [+] = Mark)
Three Array in array
The array is not necessarily a list of keywords and values, and arrays can also be placed in arrays.
Copy CodeThe code is as follows:
$produces = Array (
Array (' Apple ', 6,28.8),
Array (' pear ', 3,15.6),
Array (' banana ', 10,4.6)
);
echo $produces [0][0]. ' | '. $produces [0][1]. ' | '. $produces [0][2]. '
';
echo $produces [1][0]. ' | '. $produces [1][1]. ' | '. $produces [in a few]. '
';
echo $produces [2][0]. ' | '. $produces [2][1]. ' | '. $produces [2][2]. '
';
?>

Output
apple|6|28.8
pear|3|15.6
banana|10|4.6
To print an array in a group with a For loop
Copy CodeThe code is as follows:
$produces = Array (
Array (' Apple ', 6, 28.8),
Array (' Pear ', 3, 15.6),
Array (' banana ', 10, 4.6)
);
for ($i = 0; $i < count ($produces); $i + +)
{
for ($j = 0; $j < count ($produces [$i]); $j + +)
{
echo ' | '. $produces [$i] [$j];
}
Echo '
';
}
?>

Output
|apple|6|28.8
|pear|3|15.6
|banana|10|4.6
Two-dimensional arrays
Copy CodeThe code is as follows:
$produces = Array (
Array (' name ' = = ' apple ', ' amount ' = + 6, ' price ' = + 28.8),
Array (' name ' = ' pear ', ' amount ' = + 3, ' price ' = 15.6),
Array (' name ' = ' banana ', ' amount ' and ' = ', ' price ' = 4.6)
);
while (!! List ($key, $value) =each ($produces))
{
while (!! List ($key 2, $value 2) =each ($value))
{
echo ' | '. $key 2. ' = '. $value 2;
}
Echo '
';
}
?>

Output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
It's easier to print with foreach (recommended)
Copy CodeThe code is as follows:
$produces = Array (
Array (' name ' = = ' apple ', ' amount ' = + 6, ' price ' = + 28.8),
Array (' name ' = ' pear ', ' amount ' = + 3, ' price ' = 15.6),
Array (' name ' = ' banana ', ' amount ' and ' = ', ' price ' = 4.6)
);
foreach ($produces as $key 1 = $value 1)
{
foreach ($value 1 as $key 2 = $value 2)
{
echo ' | '. $key 2. ' = '. $value 2;
}
Echo '
';
}
?>

Output
|name=>apple|amount=>6|price=>28.8
|name=>pear|amount=>3|price=>15.6
|name=>banana|amount=>10|price=>4.6
Four Sorting of arrays
1.Sort () sort the English of the function
Copy CodeThe code is as follows:

$fruits = Array (' Lemo ', ' banana ', ' apple ', ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
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.Sort () Sorting of Chinese by function
Copy CodeThe code is as follows:

$fruits = Array (' Lemon ', ' banana ', ' apple ', ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Sort ($fruits);
echo ' sorted array: ';
Print_r ($fruits);
?>

Output:
Original array: Array ([0] + = lemon [1] = banana [2] = = Apple [3] = pear)
Sorted array: Array ([0] = lemon [1] = pear [2] = apple [3] = banana)
3. Asort-Sort the array and keep the index relationship
Copy CodeThe code is as follows:

$fruits = Array (' A ' + ' lemon ', ' b ' = ' banana ', ' c ' = ' apple ', ' d ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Asort ($fruits);
echo ' sorted array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([a] + + lemon [b] = banana [C] + apples [d] + pears)
Sorted array: Array ([a] + = lemon [d] + pears [c] + apples [b] = bananas)
4. Ksort--Sort the array by key name
Copy CodeThe code is as follows:

$fruits = Array (' b ' = ' lemon ', ' a ' = ' banana ', ' d ' = ' apple ', ' c ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Ksort ($fruits);
echo ' sorted array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([b] = lemon [a] + banana [d] + apples [c] + pears)
Sorted array: Array ([a] + = banana [b] = lemon [c] + pear [d] + apple)
5. Rsort--reverse order of the logarithm group
Copy CodeThe code is as follows:

$fruits = Array (' Lemon ', ' banana ', ' apple ', ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Rsort ($fruits);
echo ' sorted array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([0] + = lemon [1] = banana [2] = = Apple [3] = pear)
Sorted array: Array ([0] = bananas [1] = apples [2] = pears [3] + lemon)
6. Arsort--Reverse sorting an array and keep the index relationship
Copy CodeThe code is as follows:

$fruits = Array (' A ' + ' lemon ', ' b ' = ' banana ', ' c ' = ' apple ', ' d ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Arsort ($fruits);
echo ' sorted array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([a] + + lemon [b] = banana [C] + apples [d] + pears)
Sorted array: Array ([b] = banana [c] = Apple [d] = pear [a] + lemon)
7. Krsort--reverse order of array by key name
Copy CodeThe code is as follows:

$fruits = Array (' A ' + ' lemon ', ' b ' = ' banana ', ' c ' = ' apple ', ' d ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Krsort ($fruits);
echo ' sorted array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([a] + + lemon [b] = banana [C] + apples [d] + pears)
Sorted array: Array ([d] = pears [c] + apples [b] = bananas [a] + lemon)
8. Shuffle--Disturb the array
Copy CodeThe code is as follows:

$fruits = Array (' A ' + ' lemon ', ' b ' = ' banana ', ' c ' = ' apple ', ' d ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Shuffle ($fruits);
echo ' scrambled array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([a] + + lemon [b] = banana [C] + apples [d] + pears)
Scrambled array: Array ([0] + = banana [1] = Apple [2] = lemon [3] = pear)
9. Array_reverse--Returns an array of cells in reverse order
Copy CodeThe code is as follows:

$fruits = Array (' A ' + ' lemon ', ' b ' = ' banana ', ' c ' = ' apple ', ' d ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
$fruits = Array_reverse ($fruits);
echo ' inverted array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([a] + + lemon [b] = banana [C] + apples [d] + pears)
Inverted array: Array ([d] + pears [c] + apples [b] = banana [a] + lemon)
Array_unshift-inserting one or more cells at the beginning of an array
Copy CodeThe code is as follows:

$fruits = Array (' A ' + ' lemon ', ' b ' = ' banana ', ' c ' = ' apple ', ' d ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Array_unshift ($fruits, ' tomatoes Son ');
echo ' inserted array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([a] + + lemon [b] = banana [C] + apples [d] + pears)
Inserted array: Array ([0] = = tomatoes [a] + = Lemon [b] + banana [c] = + Apple [d] = pear)
Array_shift--Move the cells at the beginning of the array to a group
Copy CodeThe code is as follows:

$fruits = Array (' A ' + ' lemon ', ' b ' = ' banana ', ' c ' = ' apple ', ' d ' = ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Array_shift ($fruits);
Echo ' moved out of the array: ';
Print_r ($fruits);
?>

Output
Original array: Array ([a] + + lemon [b] = banana [C] + apples [d] + pears)
Removed array: Array ([b] = banana [c] = Apple [d] = pear)
Array_rand--random extraction of one or more cells from an array
Copy CodeThe code is as follows:

$fruits = Array (' Lemon ', ' banana ', ' apple ', ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
$NEWARR _key = Array_rand ($fruits, 2);
echo ' Random array: ';
echo $fruits [$newArr _key [0]]. ' ';
echo $fruits [$newArr _key [1]];
?>

Output
Original array: Array ([0] + = lemon [1] = banana [2] = = Apple [3] = pear)
Random array: Pear apples
Array_pop-pops the last element of the array (out of the stack)
Copy CodeThe code is as follows:

$fruits = Array (' Lemon ', ' banana ', ' apple ', ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Array_pop ($fruits);
Echo ' popup after the array: ';
Print_r ($fruits);
?>

Output:
Original array: Array ([0] + = lemon [1] = banana [2] = = Apple [3] = pear)
Array after pop-up: Array ([0] = lemon [1] = banana [2] = apple)
Array_push--pressing one or more cells into the end of the array (into the stack)
Copy CodeThe code is as follows:

$fruits = Array (' Lemon ', ' banana ', ' apple ', ' pear ');
Echo ' original array: ';
Print_r ($fruits);
Echo '
';
Array_push ($fruits, ' tomatoes Son ');
Echo ' popup after the array: ';
Print_r ($fruits);
?>

Output:
Original array: Array ([0] + = lemon [1] = banana [2] = = Apple [3] = pear)
The array after the popup: Array ([0] = lemon [1] = banana [2] = = Apple [3] = pear [4] = tomatoes)
Five manipulation of pointers to arrays
Each--Returns the current key/value pair in the array and moves the array pointer one step forward
Current--Returns the cell in the array
Reset--point the inner pointer of the array to the first cell
End--Points the inner pointer of the array to the last cell
Next-Moves the inner pointer in the array forward one
POS--alias for current ()
Prev--Rewind The internal pointer of the array back to a

Copy CodeThe code is as follows:
$fruits = Array (' Lemon ', ' banana ', ' apple ', ' pear ');
Print_r ($fruits);
Echo '
';
Echo ' each (): ';
Print_r (each ($fruits));
Echo '
';
Echo ' current (): ';
Echo (current ($fruits));
Echo '
';
Echo ' Next (): ';
Echo (Next ($fruits));
Echo '
';
Echo ' End (): ';
Echo (End ($fruits));
Echo '
';
Echo ' prev (): ';
Echo (prev ($fruits));
Echo '
';
Echo ' POS (): ';
Echo (POS ($fruits));
Echo '
';
?>

Output:
Array ([0] = lemon [1] = banana [2] = = Apple [3] = pear)
Each (): Array ([1] = lemon [value] + lemon [0] = 0 [key] + 0)
Current (): Banana
Next (): Apple
End (): Pear
Prev (): Apple
POS (): Apple
Six Number of statistics arrays
Count--count the number of cells in an array or the number of attributes in an object
sizeof--The alias of Count ()
Array_count_values--Number of occurrences of all values in the statistics array
Copy CodeThe code is as follows:
$nums = Array (1, 3, 5, 1, 3, 4, 5, 65, 4, 2, 2, 1, 4, 4, 1, 1, 4, 1, 5, 4, 5, 4);
echo count ($nums);
Echo '
';
echo sizeof ($nums);
Echo '
';
$arrayCount = Array_count_values ($nums);
Print_r ($arrayCount);
?>

Output
22
22
Array ([1] = 6 [3] = 2 [5] = 4 [4] = 7 [All] = 1 [2] = 2)
Seven Convert an array to a scalar quantity: Extract ()
Each element in the array is converted to a variable whose name is the key of the array element, and the value of the variable is an element of value.
Copy CodeThe code is as follows:
$fruits = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' o ' = ' orange ');
Extract ($fruits);
echo $a. '
';
echo $b. '
';
echo $o. '
';
?>

Output
Apple
Banana
Orange

http://www.bkjia.com/PHPjc/324807.html www.bkjia.com true http://www.bkjia.com/PHPjc/324807.html techarticle A. What is an array array is a set of elements that have some common characteristics, including similarity and type. Each element is distinguished by a special identifier, called key, and each key has a ...

  • 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.