In programming to look up the manual is indispensable, so you will learn to use the existing things, as in PHP, the array processing function already has a sort function, why write something is a hard to write bubble or heap row or fast row.
Programming is an indirect process and a process of reuse, to write good code is not without design patterns to do support, may be for beginners to learn the design pattern is a little hard (as I saw the design pattern, really a bit of a struggle), but when you have a certain amount of code accumulation, in the study of design patterns, Feeling design patterns are really useful and can help you write beautiful code. Said to say a bit run away, or to sum up PHP in the log group operation of the common functions bar.
The following summary of the array of commonly used functions, perhaps some readers will feel a little bit, sching, if you feel there are other commonly used array processing functions, to leave a comment, do not skimp on their own knowledge, and others to share things is not a very happy thing. Also, the following code from my hand, but two years ago, wrote code, welcome to criticize correct.
Array_splice () deletes the specified element in the array
Array_splice (array name, the number of deleted in the past, the size of the new array); There is no third parameter, there is no return of the array, without the third parameter, the meaning of the second argument for the past to retain a few
Exp:
<?php
$my _array=array ( //build array
"hehe" => "haha",
"A" => "Lu",
"Lu" => "GE"
);
$new =array_splice ($my _array,1,3); Use Array_splice (array name, the number of previously deleted, new an array size);
Var_dump ($new);
? >
Result: Array (2) {["A"]=> string (2) "Lu" ["Lu"]=> string (2) "GE"}
2. The traversal of the foreach () array
Usage: foreach (array as key => key value) or foreach (array as key value)
Exp:
<?php
$my _array=array ( //build array
"hehe" => "haha",
"A" => "Lu",
"Lu" => "GE"
);
foreach ($my _array as $key => $value)
{
echo $key. " => ". $value." <br/> ";
}
? >
Output results:
3, sorting the array
(1) sort () and rsort () key values sorted () from small to large, rsort () from large to low
Sort () Exp:
<?php
$my _array=array (1,2,3,6,7,8,9,4,5);//Set Array
sort ($my _array);
foreach ($my _array as $keys => $value)
{
echo $keys. " => ". $value." <br/> ";
}
? >
Output results:
0=>1
1=>2
2=>3
3=>4
4=>5
5=>6 6=>7 7=>8 8=> 9
Rsort () Exp:
<?php
$my _array=array (1,2,3,6,7,8,9,4,5);//build Array
rsort ($my _array)
; foreach ($my _array as $keys => $value)
{
echo $keys. " => ". $value." <br/> ";
}
? >
Output results:
0=>9
1=>8
2=>7
3=>6
4=>5
5=>4 6=>3 7=>2 8=> 1
(2). Asort () and Arsort () are the same as the above principles, but do not change the corresponding relationship between the key name and the key value
Exp:
<?php
$my _array=array (1,2,3,6,7,8,9,4,5);//build Array
asort ($my _array)
; foreach ($my _array as $keys => $value)
{
echo $keys. " => ". $value." <br/> ";
}
? >
Output results:
0=>1
1=>2
2=>3
7=>4
8=>5
3=>6 4=>7 5=>8 6=> 9
(3) Ksort () and Krsort () are the size ordering of the key names
4. The mathematical class function of the array
Array_sum () calculates the number of elements with count () for all key values of the array
Exp:
<?php
$my _array=array (1,2,3,6,7,8,9,4,5);//set Array
echo array_sum ($my _array);
? >
Output Result: 45
5. Other functions
Array_unique () Removes the same element from the array
In_array () detects whether a value is in the array (returns True and false)
Array_search () returns a key or value that returns the key name of the key value
Shuffle () disturb the original array
<?php
$my _array=array (1,2,3,6,7,8,9,4,5,5,5,5);//Establish array
array_unique ($my _array);//Remove the same element from the array
var_ Dump ($my _array);
echo "<br/>";
Echo In_array (5, $my _array);
echo "<br/>";
$new =array_search (6, $my _array);//returns the key name corresponding to the key value
echo $new;
? >
Output results:
array {[0]=> int (1) [1]=> int (2) [2]=> int (3) [3]=> int (6) [4]=> Int (7) [5]=> Int (8) [6]=> Int (9 [7]=> Int (4) [8]=> int (5) [9]=> int (5) [10]=> int (5) [11]=> int (5)}
1
3