Introduction to common PHP array Functions

Source: Internet
Author: User
Tags array sort mathematical functions
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. PHP has a large number of functions, all of which are remembered.

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't Repeat Yourself) principle. There are a lot of functions in PHP, all of which are remembered.

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 => 11 => 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 => 64 => 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

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.