PHP Arrays and String functions

Source: Internet
Author: User

1.1 functions on array manipulation

1.1.1Range (start,end)

Creates an array with values starting from start and ending at end

If the parameter in range is a number, then he sorts it directly in the order of the number size, and if the argument is a character, it will be sorted in ASCII-encoded order.

1.1.2Array_rand ()

Randomly take out an array of subscripts

Array_rand has a second parameter, which by default is 1, which means to get the subscript of an array element whose data type is equal to the data type of the subscript, but if you randomly get the subscript for multiple elements at once, he returns an array.

Although he randomly gets the subscript for an array element, the resulting result must be a small subscript placed in front of a large subscript and placed behind.

1.1.3Shuffle ()

Function: Disrupts the ordering of an array.

Using shuffle to scramble an array without re-copying it automatically disrupts the order of the elements in the original array.

1.1.4Is_array ()

Function: Determines whether a variable is an array data type.

Syntax: Is_array (variable name), if it is an array, returns a true if it is not an array, returns a false.

1.1.5Array_search ()

Function: According to the value of an array element, to find his relative subscript

Syntax: Array_search (value, $arr [, Boolean]), which returns the subscript of the element value

Array_search to go through the array to find the subscript, is looking from left to right, when the first matching element is found, the direct return to the subscript, the function ends.

Array_search to iterate over an array, case-sensitive

Array_search By default, the data type is not distinguished, and if you want to differentiate it from the data type, modify the third parameter of Array_search to True.

1.2 functions on array element manipulation

1.2.1Array_keys ()

Function: Gets all the subscripts in the array, returns the result or an array

Syntax: Array_keys ($arr)

1.2.2array_values ()

Function: Gets all the values in the array, the returned result is an array

Syntax: Array_values ($arr)

1.2.3array_combine ()

Function: Merges two arrays, the values of the elements in the first array are treated as subscripts of the new array elements, and the values of the elements in the second array are treated as values of the new array elements.

Syntax: Array_combine ($arr 1, $arr 2)

Array_combine to merge two arrays, the number of two array elements must be the same.

1.2.4In_array ()

Function: Finds whether the value of an array element exists.

Syntax: In_array (value, $arr [, Boolean]). Returns a Boolean value that, if present, returns a true, if not present, the returned result is false.

The data type is not differentiated by default, and if you want to differentiate, modify the third parameter to True.

In_array Case Sensitive

1.2.5array_key_exists ()

Function: Finds whether a subscript exists in an array element

Syntax: array_key_exists ($key, $arr)

Case sensitive

Array_key_exists has only two parameters , he does not differentiate between data types

1.2.6isset ()

Determines whether a variable is defined.

thinking: What is the difference between isset () and array_key_exists () Judging array elements?

Answer: If the value of an array element is NULL, Isset () determines that the result is false and array_key_exists () evaluates to True.

1.2.7implode ()

function: To stitch an array into a string.

Syntax: Implode (string, $arr)

If you omit the punctuation, he is stitching by default in an empty string

1.2.8explode ()

Function: Splits a string into an array.

Syntax: Explode (String, $str [, Num])

Explode if there is a third argument, it splits the string into several, and the last copy contains all the parts that follow the word nginx.

Explode the third parameter, if it is a negative number, means that the last few items are discarded

orthogonal difference of 1.3 array

1.3.1array_merge ()

Effect: Merges several arrays and then generates a new array.

If the index of the merged array is a number, then he ignores the subscript, generates a new array directly, and then the subscript starts with 0 reordering.

If the index of the merged array is a string, then he retains the original subscript, the merged array, and if there is a conflict in the subscript, then the value will replace the original value.

1.3.2Array_diff ()

The difference set of code that computes elements that are not in the other array in an array.

Syntax: Array_diff ($arr 1, $arr 2[, $arr 3.. $ARRN])

Computes the part of the first parameter array that is not in the other array

The subscript in the Get element is the subscript of the first parameter array element.

1.3.3array_intersect ()

Computes the intersection of an array

Syntax: Array_intersect ($arr 1, $arr 2[, $arr 3.. $ARRN]);

Computes the part that is common in all arrays, and the subscript is still subject to the subscript of the first parameter array.

1.4 functions to iterate through an array

1.4.1Array_map () callback function

Syntax: Array_map (name of function, $arr)

Function: To remove the value of each element in the array, and then to execute the function.

In addition to calling PHP built-in functions, we can also go to the handwriting function, and then through the function name of our custom function, can also implement a function called function

In addition to the pattern of writing function names, we can also write an anonymous function on our own, the effect is the same.

We can also pass multiple arrays at once, then the first element of each array is taken out of the value, and the second will take the value of the second element of each array, and so on

If the function called in Array_map is empty, then he will take the elements in the array sequentially, then merge them into a new array, and the final result is a two-dimensional array.

1.4.2Array_walk ()

Function: Remove the subscript and value of the array element in turn.

Syntax: Array_walk ($arr, ' function name ')

Array_map can pass multiple arrays at once, but Array_walk can only pass an array.

Array_map is more common in the work, Array_walk may appear smaller, because the Array_walk function can be replaced by foreach.

1.5 stacks and queues

Stack: Advanced post-out, LIFO

Queue: FIFO, LIFO

1.5.1Array_push ()

Function: Inserts an element toward the end of the array

Syntax: Array_push ($arr, $value [, $value 2.. $valuen])

Array_push can also add multiple elements at once.

1.5.2Array_pop ()

Function: Delete the last element in the array

Syntax: Array_pop ($arr)

Array_pop the element is removed, the returned result is the value of the removed element

1.5.3array_shift ()

Function: Delete the first element inside the array

Syntax: Array_shift ($arr)

After moving out the first element, the subsequent element will move forward one bit, and the returned result is also the value of the removed element.

1.5.4array_unshift ()

Function: Inserts an element to the front of the array

Syntax: Array_unshift ($arr, $value [, $value 2.. $value 3])

Think: What's the difference between array_shift () and using unset () to remove the first element in the array?

Answer: unset () removes the value of the array element, but retains the subscript, but Array_shift () moves the element's subscript and value out uniformly.

1.6 Sorting of arrays

1.6.1sort ()

Function: Sorts the values of the array elements in order from small to large.

Syntax: Sort ($arr)

Using sort to sort the array, the subscript of the array is all out, and the newly generated array subscript starts at 0 and increments in sequence.

1.6.2rsort ()

Function: Sorts an array from large to small.

Syntax: Rsort ($arr)

1.6.3asort ()

Function: Sorts an array from small to large, but retains the original subscript.

Syntax: Asort ($arr)

1.6.4arsort ()

Function: Sorts an array from large to small, but also retains the subscript.

Syntax: Arsort ($arr)

1.6.5ksort ()

Function: Sort the subscript of an array from small to large

Syntax: Ksort ($arr)

1.6.6krsort ()

function: Sorts the subscript of an array from large to small

Syntax: Krsort ($arr)

1.6.7natsort ()

Function: Natural sorting treats a string as a whole and then sorts from small to large.

Syntax: Natsort ()

1.6.8usort ()

Function: Customize a sort.

Syntax: Usort ($arr, FN ($num 1, $num 2) {})

If the return is an integer greater than 0, then the $num 1 will be sorted backwards

If the return is an integer less than 0, then the $num 1 will be sorted forward

If the return is an integer equal to 0, then he remains the same

Sometimes, for some two-dimensional arrays, we are difficult to implement in the ordinary sort order, at this time we can go to use custom sort.

1.7 string Manipulation

1.7.1[]

Syntax: [num]

Function: Gets the num+1 byte

1.7.2strlen ()

Function: Gets how many bytes the string occupies

Syntax: strlen ($STR)

The Chinese symbol also accounts for three bytes

1.7.3trim ()

Function: Remove space between the left and right sides of the string

Syntax: Trim ($STR, [Char]), and the result is a string that strips away space.

Optional parameters, filter characters can also be specified by the charlist parameter. Generally you want to list all the characters you want to filter, or you can use ".." To list a range of characters.

1.7.4LTrim ()

Function: Remove the space to the left of the string

Syntax: LTrim ($STR, [char])

1.7.5RTrim ()

Function: Remove the space to the right of the string

Syntax: RTrim ($STR, [char])

1.7.6Strpos ()

Function: Find the position where a character first appears. Note that the judgment is all equal to false in order to fail. Because subscript 0 will be judged equal to false.

Syntax: Strpos ($STR, Char[,num])

If more than one qualifying character is present, he only strokes the first one that matches the required character.

The strpos lookup string is searched from left to right, and the third parameter represents the position from which to start looking, case-sensitive.

1.7.7Strrpos ()

Function: Search from right to left, find the first qualifying character end

Syntax: Strrpos ($STR, Char[,num])

The third parameter represents the location where the search ended, and if not found, returns a false

1.7.8str_repeat ()

function: How many times a string is repeated

Syntax: str_repeat ($STR, num)

1.7.9chr ()

Function: Converts a decimal number to the corresponding ASCII encoding

1.7.10Ord ()

Function: Converts an ASCII encoding to the corresponding number

1.7.11substr ()

Role: Intercepting a string

Syntax: substr ($STR, startnum[, length of intercept])

If the third argument is omitted, it is intercepted directly to the last

The role of parameters

1.7.12STRCHR ()

Function: Start interception from a character

Syntax: STRCHR ($STR, Char[,boolean])

The third parameter represents whether the STRCHR intercept character is the right or left part of the string, the default is False, represents the right part, and retains the intercepted character, or, if modified to true, the left part is intercepted and the intercepted character is not preserved. Strstr is another name for STRCHR. is case sensitive.

1.7.13stristr ()

Function: Intercepts a string from a character, but he is not case sensitive.

Syntax: Stristr ($STR, Char[,boolean])

Whether it is strstr or STRISTR, the intercept string is searched from left to right, and the first qualifying character is found.

1.7.14strrchr ()

Function: begins to intercept a string from a character, but he is looking from right to left.

Syntax: STRRCHR ($STR, Char)

1.7.15str_replace ()

Role: Replacing bytes in a string

Syntax: str_replace (replace pre-character, replaced character, $STR)

If you want to replace multiple bytes at once, place the bytes that need to be replaced in an array

If you want to replace more than one character with a single character, before using an array, you do not need to use an array.

The order of substitution is to replace the first one before the first one is replaced, and then the second element is replaced.

1.7.16str_split ()

Function: Splits a string into an array.

Syntax: Str_split ($arr, num)

Str_split () splits a string, which is a string that is split to restrict the length of the character.

1.7.17Str_pad ()

Function: Fills a string with a specific content.

Syntax: Str_pad ($STR, Length,char)

Str_pad There is a fourth argument, and his value is a constant.

Str_pad_left: Add padding to the left

Str_pad_right: Add padding to the right (default)

Str_pad_both: Add padding to both sides

1.7.18strtolower ()

Function: Converts a string to full lowercase

1.7.19strtoupper ()

Function: Converts a string to full capitalization

1.7.20nl2br ()

Effect: convert \ n to

1.7.21htmlspecialchars ()

Function: parsing special characters into ordinary characters

1.7.22UrlEncode ()

Encrypt the encoding.

1.7.23UrlDecode ()

To decrypt the code.

1.8 Multi-character processing function

Before using the multi-character processing function, you must first open the appropriate configuration, first open php.ini, then search php_mbstring, remove the comments, restart Apache can

1.8.1Mb_strlen ()

Returns the number of characters that a string has altogether.

Syntax: Mb_strlen ($STR, encoded format)

1.8.2mb_internal_encoding ()

If no parameters are added, the direct output represents the current encoding format.

If you add a parameter, it represents modifying its character set encoding.

1.8.3Mb_strpos ()

Gets the position where a character appears.

1.8.4mb_substr ()

Intercept string

1.8.5iconv ()

Function: Sets the character set encoding of a string

Syntax: Iconv (character set encoding before setting, character set encoding after setting, string to be modified) a new string is generated when the result is modified.

PHP Arrays and String 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.