Introduction to common PHP array functions, php array Functions

Source: Internet
Author: User
Tags array sort mathematical functions

Introduction to common PHP array functions, php array Functions

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. "<br/>" ;}?>

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. "<br/>" ;}?>

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. "<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 () 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. "<br/>" ;}?>

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 "<br/>"; echo in_array (5, $ my_array); echo "<br/> "; $ 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


How many of the most common functions are in the php array?

Foreach (array_expression as $ key => $ value)
The key () function is used to read the index value current () of the data pointed to by the current pointer. This function is used to obtain the data at the current pointer position.
The count () function returns the length value of the target array.
Array pointer control functions
Pnext (), prev (), end (), and reset ()
Next () is responsible for moving the pointer backward, prev () is responsible for moving the Pointer Forward; end () will point the pointer to the last element in the array, and reset () the function moves the current pointer to the first index position unconditionally.
In_array (mixed $ needle, array $ haystack [, bool $ strict]); determines whether an array element exists.
Array_search (), which returns the corresponding key name. The array_key_exists () function can retrieve whether the given key name (INDEX) exists in the array.

Also, if necessary, I can give you a PPT

Common php Functions

In fact, there is no need to search for the so-called "common functions". The so-called common functions vary from person to person. Someone often uses a function, but it does not mean that you will always use it. There are a lot of php functions. The key is to keep the help manual for php. If you are not familiar with it, you can check it more often and it will naturally become "common.

In general, functions of strings, arrays, and databases use a large number of classes.

The following lists the functions for your reference only.

========================================================== ====

/// ================================================ ======================================
// Y returns the last two digits of the Year. Y indicates the four digits of the Year. m indicates the number of the month. M indicates the English Number of the month. D: The number of months. D: the number of weeks.
$ Date = date ("Y-m-d ");

// Include, include_once.require, require_once
// Require ("file. php") reads the file specified by require before executing the PHP program. If an error occurs, it is fatal.
// Include ("file. php") can be stored in any position of the PHP program. Only when the PHP program is executed can the file specified by include be read. If an error occurs, a prompt is displayed.

// ==================================== Output print ======== ======================================
// Sprintf ("% d", "3.2"); // format only. The formatted string is returned without output.
// Printf ("% d", "3.2"); // format and Output
// Print ("3.2"); // output only
// Echo "nihao", "aa"; // multiple strings can be output.
// Print_r (array ("a", "B", "c"); // displays the key values and elements of the array in sequence.

// ================================ Common string functions ====== ======================================

// Obtain the length of a string, including the number of characters and spaces.
$ Str = "sdaf sd ";
$ Len = strlen ($ str );

// Use the string in the first parameter to connect each element in the following array and return a string.
$ Str = implode ("-", array ("a", "B", "c "));

// String segmentation method. An array is returned. The string is separated by the characters in the first parameter, and the front and back of and between the specified characters are intercepted, if the specified character starts or ends, the element at the beginning or end of the returned array is an empty string.
// Returns a null value to the element corresponding to the array if the string is not split. The last limit returns the length of the array. If there is no limit, the array will be split.
$ Array = explode ("a", "asdd ...... remaining full text>

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.