How can we reduce the number of arrays in programming? the following are commonly used array processing functions in PHP. In programming, we should follow the DRY (Don 'trepeatyourself) principle. There are a lot of functions in PHP, and it is not realistic to remember these functions, but common functions must be used skillfully, the manual is indispensable in programming, so you should learn to use the existing items, just as the array processing function in PHP already has a sorting function, why is it hard to write bubble, heap, or fast data.
Programming is an indirect process and a process of reuse. to write good code, the design pattern is indispensable for support, it may be difficult for beginners to learn the design patterns (just like when I first looked at the design patterns, it was a little difficult), but when you have accumulated a certain amount of code, when studying the design patterns, I feel that the design pattern is really useful and can help you write beautiful code. It's a bit biased. Let's summarize the common functions for array operations in php.
Some readers may feel a little less about the commonly used functions in the array summarized below. Are there any other commonly used array processing functions? leave a comment, don't be stingy with your own knowledge. isn't it a pleasure to share something with others. Also, the code below is from my own hand, but it was written two years ago. You are welcome to criticize and correct it.
Array_splice () deletes the specified element from the array.
Array_splice (array name, number of previously deleted items, size of a new array); if there is no third parameter, No returned array is returned. if there is no third parameter, the second parameter indicates the number
Exp:
<? Php $ my_array = array (// create an array "hehe" => "haha", "A" => "lu", "lu" => "ge "); $ new = array_splice ($ my_array, 1, 3); // use array_splice (array name, number of previously deleted records, size of a new array); var_dump ($ new);?>
Result: array (2) {["A"] => string (2) "lu" ["lu"] => string (2) "ge "}
2. traverse the foreach () array
Usage: foreach (array as key name => key value) or foreach (array as key value)
Exp:
<? Php $ my_array = array (// create an array "hehe" => "haha", "A" => "lu", "lu" => "ge "); foreach ($ my_array as $ key => $ value) {echo $ key. "=> ". $ value."
";}?>
Output result:
hehe=>hahaA=>lulu=>ge
3. sort arrays
(1) sort () and rsort () values from small to large, and rsort () from large to small
Sort () exp:
<? Php $ my_array = array (1, 2, 3, 6, 7, 8, 9, 4, 5); // create an array sort ($ my_array); foreach ($ my_array as $ keys => $ value) {echo $ keys. "=> ". $ value."
";}?>
Output result:
0=>1 1=>2 2=>3 3=>4 4=>5 5=>6 6=>7 7=>8 8=>9
Rsort () exp:
<? Php $ my_array = array (, 5); // create an array rsort ($ my_array); foreach ($ my_array as $ keys => $ value) {echo $ keys. "=> ". $ value."
";}?>
Output result:
0=>9 1=>8 2=>7 3=>6 4=>5 5=>4 6=>3 7=>2 8=>1
(2). asort () and arsort () follow the same principle as above, but do not change the correspondence between the key name and the key value.
Exp:
<? Php $ my_array = array (, 5); // create an array asort ($ my_array); foreach ($ my_array as $ keys => $ value) {echo $ keys. "=> ". $ value."
";}?>
Output result:
0=>11=>22=>37=>48=>53=>64=>75=>86=>9
(3) ksort () and krsort () are the sizes of key names.
4. array mathematical functions
Array_sum () is used to calculate the sum of all key values of the array and the count () value to calculate the number of elements.
Exp:
<? Php $ my_array = array (1, 2, 3, 6, 7, 8, 9, 4, 5); // create an array echo array_sum ($ my_array);?>
Output result: 45
5. other functions
Array_unique () removes the same element from the array
In_array () checks whether a value is in the array (returns true and false)
Array_search () returns the key or value, and returns the key name corresponding to the key value.
Shuffle () disrupts the original array
<? Php $ my_array = array (1, 2, 3, 6, 7, 8, 9, 4, 5, 5); // create an array array_unique ($ my_array ); // remove the same element var_dump ($ my_array); echo"
"; Echo in_array (5, $ my_array); echo"
"; $ New = array_search (6, $ my_array); // The Returned key name echo $ new;?>
Output result:
array(12) { [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) } 13