Chapter 5 php array operations

Source: Internet
Author: User
Tags pear pears
An array is a group of elements with some common characteristics, including similarity and type. Each element is distinguished by a special identifier, called a key, and each key has a value 1. what is an array?
An array is a group of elements with some common characteristics, including similarity and type.
Each element is distinguished by a special identifier, called a key, and each key has a value
1. two ways to create an array:
1.1 Use the array () function

The code is as follows:


$ Usernames = array ('aler', 'Mary ', 'Lucy', 'Bob', 'Jack', 'John', 'mark ');
Foreach ($ usernames as $ name)
{
Echo $ name .'
';
}
?>


Output
Alerk
Mary
Lucy
Bob
Jack
John
Mark
1.2 Use the range () function

The 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. two methods to access array elements cyclically:
2.1 for loop

The 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

The 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 value of the array.

The code is as follows:


$ Letters = range ('A', 'G', 2 );
Foreach ($ letters as $ key => $ value)
{
Echo $ key. '---'. $ value .'
';
}
?>


Output
0 ---
1 --- c
2 --- e
3 --- g
3. is_array () function, used to determine whether the variable is an array

The 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

The 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] => Mark [5] => John)
The source code is displayed as follows:
Array
(
[0] => Jackie
[1] => Mary
[2] => Lucy
[3] => Bob
[4] => Mark
[5] => John
)
II. Custom key array
1. if you do not want to create an array whose Default subscript is zero, you can use the following method to create an array whose key is a string.

The code is as follows:


// Initialize the array
$ Userages = array ('Jack' => 23, 'Lucy '=> 25, 'mark' => 28 );
// Access each element of the array
Echo $ userages ['Jack'].'
';
Echo $ userages ['Lucy '].'
';
Echo $ userages ['Mark'].'
';
?>


2. append an element to a custom key group

The code is as follows:


// Initialize the 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.

The code is as follows:


// Add directly without creating an array
$ Ages ['Jack'] = 23;
$ Ages ['Lucy '] = 25;
$ Ages ['Mark'] = 28;
Foreach ($ ages as $ key => $ value)
{
Echo $ key. '----'. $ value .'
';
}
?>


4. Use of loop print array foreach

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

The code is as follows:


$ Ages ['Jack'] = 23;
$ Ages ['Lucy '] = 25;
$ Ages ['Mark'] = 28;
$ A = each ($ ages );
Print_r ($ );
Echo'
';
$ A = each ($ ages );
Print_r ($ );
Echo'
';
$ A = each ($ ages );
Print_r ($ );
?>


Use the each () function for loop printing

The code is as follows:


$ Ages ['Jack'] = 23;
$ Ages ['Lucy '] = 25;
$ Ages ['Mark'] = 28;
While (!! $ Element = each ($ ages ))
{
Print_r ($ element );
Echo'
';
}
?>


Another printing method

The code is as follows:


$ Ages ['Jack'] = 23;
$ Ages ['Lucy '] = 25;
$ Ages ['Mark'] = 28;
While (!! $ Element = each ($ ages ))
{
Echo $ element ['key']. '=>'. $ element ['value'];
Echo'
';
}
?>


6. use of the list () function -- assign values in the array to some variables

The code is as follows:


$ Ages ['Jack'] = 23;
$ Ages ['Lucy '] = 25;
$ Ages ['Mark'] = 28;
List ($ name, $ age) = each ($ ages );
Echo $ name. '=>'. $ age;
?>


Print results cyclically with list

The 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
7. use of the reset () function -- direct the internal pointer of the array to the first unit

The 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 .'
';
// Reset the array 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 repeated values from the array

The code is as follows:


$ Nums = array (, 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 () -- exchange keys and values in the array
$ Userages = array ('Jack' => 23, 'Lucy '=> 25, 'mark' => 28 );
$ Ages = array_flip ($ userages );
Print_r ($ ages );
?>


Output
Array ([23] => Jack [25] => Lucy [28] => Mark)
3. array in the array
An array is not necessarily a list of keywords and values. an array can also be placed in an array.

The 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 [1] [2].'
';
Echo $ produces [2] [0]. '|'. $ produces [2] [1]. '|'. $ produces [2] [2].'
';
?>


Output
Apple | 6 | 1, 28.8
Pear | 3 | 1, 15.6
Bankana | 10 | 4.6
Print the array in the array with a for loop

The 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 array

The 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' => 10, 'price' => 4.6)
);
While (!! List ($ key, $ value) = each ($ produces ))
{
While (!! List ($ key2, $ value2) = each ($ value ))
{
Echo '|'. $ key2. '=>'. $ value2;
}
Echo'
';
}
?>


Output
| Name => apple | amount = & gt; 6 | price = & gt; 28.8
| Name => pear | amount = & gt; 3 | price = & gt; 15.6
| Name => banana | amount = & gt; 10 | price = & gt; 4.6
It is easier to print with foreach (recommended)

The 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' => 10, 'price' => 4.6)
);
Foreach ($ produces as $ key1 => $ value1)
{
Foreach ($ value1 as $ key2 => $ value2)
{
Echo '|'. $ key2. '=>'. $ value2;
}
Echo'
';
}
?>


Output
| Name => apple | amount = & gt; 6 | price = & gt; 28.8
| Name => pear | amount = & gt; 3 | price = & gt; 15.6
| Name => banana | amount = & gt; 10 | price = & gt; 4.6
4. sort arrays
1. Sort By Sort () function in English

The 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)
Array ([0] => apple [1] => banana [2] => lemo [3] => pear)
2. Sort Chinese by the Sort () function

The code is as follows:



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


Output:
Original Array: Array ([0] => lemon [1] => bananas [2] => Apple [3] => pears)
Array: Array ([0] => lemon [1] => pears [2] => Apple [3] => bananas)
3. asort -- sorts the array and maintains the index relationship.

The code is as follows:



$ Fruits = array ('a' => 'limit', 'B' => 'banana ', 'C' => 'apple ', 'D' => 'peez ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Asort ($ fruits );
Echo 'sorted array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([a] => lemon [B] => banana [c] => Apple [d] => pear)
Array ([a] => lemon [d] => pears [c] => Apple [B] => bananas)
4. ksort -- sort the array by key name

The code is as follows:



$ Fruits = array ('B' => 'limit', 'a' => 'banana ', 'D' => 'apple ', 'C' => 'piz ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Ksort ($ fruits );
Echo 'sorted array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([B] => lemon [a] => bananas [d] => Apple [c] => pears)
Array ([a] => bananas [B] => lemons [c] => pears [d] => apples)
5. rsort -- reverse sorting of arrays

The code is as follows:



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


Output
Original Array: Array ([0] => lemon [1] => bananas [2] => Apple [3] => pears)
Array: Array ([0] => banana [1] => Apple [2] => pears [3] => lemons)
6. arsort -- sorts arrays in reverse order and maintains the index relationship.

The code is as follows:



$ Fruits = array ('a' => 'limit', 'B' => 'banana ', 'C' => 'apple ', 'D' => 'peez ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Arsort ($ fruits );
Echo 'sorted array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([a] => lemon [B] => banana [c] => Apple [d] => pear)
Array ([B] => banana [c] => Apple [d] => pears [a] => lemons)
7. krsort -- sort the array in reverse order by key name

The code is as follows:



$ Fruits = array ('a' => 'limit', 'B' => 'banana ', 'C' => 'apple ', 'D' => 'peez ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Krsort ($ fruits );
Echo 'sorted array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([a] => lemon [B] => banana [c] => Apple [d] => pear)
Array ([d] => pear [c] => Apple [B] => banana [a] => lemon)
8. shuffle -- disrupt the array

The code is as follows:



$ Fruits = array ('a' => 'limit', 'B' => 'banana ', 'C' => 'apple ', 'D' => 'peez ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Shuffle ($ fruits );
Echo 'disordered array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([a] => lemon [B] => banana [c] => Apple [d] => pear)
Array after disruption: Array ([0] => bananas [1] => Apple [2] => lemons [3] => pears)
9. array_reverse -- returns an array with the opposite unit order.

The code is as follows:



$ Fruits = array ('a' => 'limit', 'B' => 'banana ', 'C' => 'apple ', 'D' => 'peez ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
$ Fruits = array_reverse ($ fruits );
Echo 'reverse array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([a] => lemon [B] => banana [c] => Apple [d] => pear)
Array after inversion: Array ([d] => pear [c] => Apple [B] => banana [a] => lemon)
10. array_unshift -- insert one or more units at the beginning of the array

The code is as follows:



$ Fruits = array ('a' => 'limit', 'B' => 'banana ', 'C' => 'apple ', 'D' => 'peez ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Array_unshift ($ fruits, 'employee sub ');
Echo 'inserted array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([a] => lemon [B] => banana [c] => Apple [d] => pear)
Inserted Array: Array ([0] => lemon [a] => lemon [B] => banana [c] => Apple [d] => pear)
11. array_shift -- remove elements starting with an array

The code is as follows:



$ Fruits = array ('a' => 'limit', 'B' => 'banana ', 'C' => 'apple ', 'D' => 'peez ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Array_shift ($ fruits );
Echo 'removed array :';
Print_r ($ fruits );
?>


Output
Original Array: Array ([a] => lemon [B] => banana [c] => Apple [d] => pear)
Removed Array: Array ([B] => banana [c] => Apple [d] => pear)
12. array_rand -- randomly retrieve one or more units from the array

The code is as follows:



$ Fruits = array ('Lemon ', 'banana', 'apple', 'pe ');
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] => bananas [2] => Apple [3] => pears)
Random array: Pear Apple
13. array_pop -- bring up the last unit of the array (output stack)

The code is as follows:



$ Fruits = array ('Lemon ', 'banana', 'apple', 'pe ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Array_pop ($ fruits );
Echo 'pop-up array :';
Print_r ($ fruits );
?>


Output:
Original Array: Array ([0] => lemon [1] => bananas [2] => Apple [3] => pears)
Array after the pop-up: Array ([0] => lemon [1] => banana [2] => Apple)
14. array_push -- push one or more units to the end of the array (into the stack)

The code is as follows:



$ Fruits = array ('Lemon ', 'banana', 'apple', 'pe ');
Echo 'original array :';
Print_r ($ fruits );
Echo'
';
Array_push ($ fruits, 'employee sub ');
Echo 'pop-up array :';
Print_r ($ fruits );
?>


Output:
Original Array: Array ([0] => lemon [1] => bananas [2] => Apple [3] => pears)
Array after the pop-up: Array ([0] => lemon [1] => banana [2] => Apple [3] => pear [4] => hazelnut)
5. array pointer operations
Each -- returns the current key/value pair in the array and moves the array pointer one step forward.
Current -- returns the current unit in the array.
Reset -- point the internal pointer of the array to the first unit
End -- points the internal pointer of the array to the last unit.
Next -- move the internal pointer in the array to a forward position
Alias of pos -- current ()
Prev -- returns the internal pointer of the array to one

The code is as follows:


$ Fruits = array ('Lemon ', 'banana', 'apple', 'pe ');
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] => bananas [2] => Apple [3] => pears)
Each (): Array ([1] => lemon [value] => lemon [0] => 0 [key] => 0)
Current (): Banana
Next (): Apple
End (): Pear
Prev (): Apple
Pos (): Apple
6. count the number of arrays
Count -- calculates the number of units in the array or the number of attributes in the object.
Sizeof -- count () alias
Array_count_values -- count the number of occurrences of all values in the array

The 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 [65] => 1 [2] => 2)
7. convert the array into a scalar variable: extract ()
Convert each element in the array to a variable. the variable name is the key of the array element, and the variable value is the value of the array element.

The code is as follows:


$ Fruits = array ('a' => 'apple', 'B' => 'bana', 'O' => 'Orange ');
Extract ($ fruits );
Echo $ .'
';
Echo $ B .'
';
Echo $ o .'
';
?>


Output
Apple
Banana
Orange

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.