Php development array (2)-php Tutorial

Source: Internet
Author: User
Php development array (2) I shared some basic knowledge about arrays with you in the php development array (1, in this blog, I will share with you some operations on arrays.

1,String and array conversion
Conversion of strings and arrays is often used in program development. explode () and implode () functions are mainly used for implementation.
(1) use the explode () function to cut the string according to the specified string or character separator.
The syntax format is as follows:
Array explode (stringSeparator, StringStr, [IntLimit])
Returns an array composed of strings. each element is a substring of str, which is separated by spatator as a boundary point. If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element contains the rest of str. If the separator is a null character (""), The explode () function returns false. if the value of the separator cannot be found in str, The explode () the function returns an array containing a single str element. if the limit parameter is negative, all elements except the last limit element are returned.
The sample code is as follows:

 

The running result is as follows:

(2), use the implode () function to convert the array into a string.
The syntax format is as follows:
Implode (stringGlue, ArrayArr)
The glue parameter is a string type, indicating the delimiter to be passed in. The arr parameter indicates the name of the array variable to be merged.
The sample code is as follows:

 

The running result is as follows:

2. query the specified elements in the array.
Array_search () function. specify the specified value in the array. find the return key name. otherwise, false is returned.
The syntax format is as follows:
Mixed array_search (mixed needle, array haystack [, bool strict])
The needle parameter specifies the value to be searched in the array; the haystack parameter specifies the array to be searched; the strict parameter is an optional parameter. if it is true, the type of the given value is also checked.
The sample code is as follows:

 

The running result is as follows:

3. array elements output stack
Elements in the array are arranged by index value in sequence, so that the elements in the array are not like the elements in the stack after the stack. Therefore, obtaining the last element in the array is a bit like getting out of the stack.
The array_pop () function obtains the last element of the array and deducts the length of the array by 1. If it is null or not an array, null is returned.
The syntax format is as follows:
Mixed array_pop (array arr)
The arr parameter is the name of the array variable name.
The instance code is as follows:

 

The running result is as follows:

4. add elements to the array
The array_push () function treats an array as a stack and presses the passed variables at the end of the stack. the length of the array increases the number of variables that are written into the stack, returns the number of new elements in the array.
The syntax format is as follows:
Int array_push (arrayArr, Mixed var [, mixed…])
The arr parameter is the specified array, and var is the variable to be added.
The sample code is as follows:

 

5. delete repeated elements in the array.
The array_unique () function deletes repeated elements in the array. Sort the values as strings, and retain only one key name for each key value. ignore all the key names that follow, that is, delete repeated elements in the array.
The syntax format is as follows:
Array array_unique (arrayArr)
Arr is the variable name of the array variable to delete repeated elements.

The sample code is as follows:

 

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.