PHP selection array Functions

Source: Internet
Author: User
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, most functions can be used by querying the PHP manual.

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, and it is unrealistic to remember these functions, but common functions should still be used skillfully, most functions can be used by querying the PHP manual.

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, and it is unrealistic to remember these functions, but common functions should still be used skillfully, most functions can be used by querying the PHP manual. 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.

  1. 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:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    $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 objects, 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:

1

2

3

4

5

6

7

8

9

10

11

$my_array=array( // Create an array

"hehe"=>"haha",

"A"=>"lu",

"lu"=>"ge"

);

foreach($my_array as $key=>$value)

{

echo $key."=>".$value."
"
;

}

?>

Output result:

Hehe => haha

A => lu

Lu => ge

3. Sort Arrays

(1) sort () and rsort () values from small to large, and rsort () from large to small

Sort () exp:

1

2

3

4

5

6

7

8

$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:

1

2

3

4

5

6

7

8

$my_array=array(1,2,3,6,7,8,9,4,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:

1

2

3

4

5

6

7

8

$my_array=array(1,2,3,6,7,8,9,4,5);// Create an array

asort($my_array);

foreach($my_array as $keys=>$value)

{

echo $keys."=>".$value."
"
;

}

?>

Output result:

0 => 1
1 => 2
2 => 3
7 => 4
8 => 5
3 => 6
4 => 7
5 => 8
6 => 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:

    1

    2

    3

    4

    $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

    • 1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      $my_array=array(1,2,3,6,7,8,9,4,5,5,5,5);// Create an array

      array_unique($my_array);// Remove the same element from the array

      var_dump($my_array);

      echo "
      "
      ;

      echo in_array(5,$my_array);

      echo "
      "
      ;

      $new=array_search(6,$my_array);// Return the key name corresponding to the key value

      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 )}
      1
      3

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.