PHP Featured Array functions

Source: Internet
Author: User
Tags learn php

  How can programming be less of an array, the following is the most commonly used to learn PHP array processing functions. In programming to follow a principle is dry (Don ' t Repeat yourself) principle, PHP has a large number of functions, all remember that these functions are not very realistic, but the commonly used functions are used skillfully, most of the functions of the use of the method can be queried by PHP manual to use. In the programming of the manual is not necessary, so you will learn to use the existing things, such as PHP in the array processing function has a sorting function, why still writing is to pay the strength to write bubble or heap or fast row.

Programming is an indirect process, but also the process of reuse, to write good code is not necessary to do the design pattern to do support, may be for beginners to learn the design mode is a bit difficult (as I looked at the design mode, it is a bit laborious), but when you have a certain amount of code accumulation, in the study of design patterns, Feeling the design pattern is really useful and can help you write beautiful code. Talk about a bit of a deviation, or to summarize the PHP operation of the common function of the array.

The following summarizes the array of commonly used functions, perhaps some readers will feel a little bit, crowds, if you feel there are other commonly used array processing functions, give a comment to leave Bai, do not skimp on their knowledge, and others to share things is not a very happy thing. Also, the following code from my hand, but two years ago written code, you are welcome to criticize.

  1. Array_splice () deletes the specified element in the array

    Array_splice (array name, the number of the previous delete, the size of the new array); There is no third parameter, there is no return array, no third argument, the meaning of the second argument is to keep a few in the past

    Exp:

    123456789 <?php    $my_array=array(    //建立数组        "hehe"=>"haha",        "A"=>"lu",        "lu"=>"ge"    );    $new=array_splice($my_array,1,3);    //使用array_splice(数组名,从前往后删的个数,new一个数组的大小);    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 name = = key value) or foreach (array as key value)

Exp:

1234567891011 <?php    $my_array=array(    //建立数组        "hehe"=>"haha",        "A"=>"lu",        "lu"=>"ge"    );    foreach($my_arrayas$key=>$value)    {        echo$key."=>".$value."<br/>";    }?>

Output Result:

Hehe=>haha

A=>lu

Lu=>ge

3. Sorting of arrays

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

Sort () Exp:

12345678 <?php    $my_array=array(1,2,3,6,7,8,9,4,5);//建立数组    sort($my_array);    foreach($my_arrayas$keys=>$value)    {        echo$keys."=>".$value."<br/>";    }?>

Output Result:

0=>1

1=>2
2=>3
3=>4
4=>5
5=>6
6=>7
7=>8
8=>9

Rsort () Exp:

12345678 <?php    $my_array=array(1,2,3,6,7,8,9,4,5);//建立数组    rsort($my_array);    foreach($my_arrayas$keys=>$value)    {        echo$keys."=>".$value."<br/>";    }?>

Output Result:

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 above, but do not change the corresponding relationship between the key name and the key value.

Exp:

12345678 <?php    $my_array=array(1,2,3,6,7,8,9,4,5);//建立数组    asort($my_array);    foreach($my_arrayas $keys=>$value)    {        echo$keys."=>".$value."<br/>";    }?>

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 size of the key names sorted

4. Mathematical class functions for arrays

    • Array_sum () calculates the number of all key values and count () elements of an array

      Exp:

      1234 <?php    $my_array=array(1,2,3,6,7,8,9,4,5);//建立数组    echoarray_sum($my_array);?>

Output results: 45

5. Other functions

    • Array_unique () Removes the same element from the array

    • In_array () detects if 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 () disrupts the original array

    • 12345678910 <?php    $my_array=array(1,2,3,6,7,8,9,4,5,5,5,5);//建立数组    array_unique($my_array);//去除数组中的相同元素    var_dump($my_array);    echo"<br/>";    echoin_array(5,$my_array);    echo "<br/>";    $new=array_search(6,$my_array);//返回的是键值所对应的键名    echo$new;?>

      Output Result:

    • 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

PHP Featured Array functions

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.