Introduction to PHP Common array functions

Source: Internet
Author: User
Tags foreach manual sort

How can programming less arrays, the following is the learning of PHP, the commonly used array processing functions. In programming to follow a principle is dry (Don ' t Repeat yourself) principle, PHP has a large number of functions, remember that these functions are not realistic, but commonly used functions are skilled, most of the use of functions can be used to query the PHP manual to use.

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:

  

$my _array=array (//Create an 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:

  

$my _array=array (//Create an array

"hehe" => "haha",

"A" => "Lu",

"Lu" => "GE"

);

foreach ($my _array as $key => $value)

{

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

}

?>

Output results:

Hehe=>haha

A=>lu

Lu=>ge

3, sorting the array

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

Sort () Exp:

  

$my _array=array (1,2,3,6,7,8,9,4,5);//Set up an array

Sort ($my _array);

foreach ($my _array as $keys => $value)

{

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

}

?>

Output results:

0=>1

1=>2

2=>3

3=>4

4=>5

5=>6

6=>7

7=>8

8=>9

Rsort () Exp:

  

$my _array=array (1,2,3,6,7,8,9,4,5);//Set up an array

Rsort ($my _array);

foreach ($my _array as $keys => $value)

{

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

}

?>

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:

  

$my _array=array (1,2,3,6,7,8,9,4,5);//Set up an array

Asort ($my _array);

foreach ($my _array as $keys => $value)

{

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

}

?>

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:

  

$my _array=array (1,2,3,6,7,8,9,4,5);//Set up an 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

  

$my _array=array (1,2,3,6,7,8,9,4,5,5,5,5);//Set up 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);//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

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.