An array of PHP development (ii)

Source: Internet
Author: User
In the PHP development of the array (a) and you share some basic knowledge of the array, in this blog and you share some of the operations of the group.

1, conversion of strings to arrays
The conversion of strings and arrays is often used in the process of program development, mainly using the explode () function and the implode () function.
(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 (string separator, string str, [int limit])
Returns an array of strings, each of which is a substring of STR, 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 will contain the remainder of Str. If separator is a null character (""), the Explode () function returns FALSE, and if the separator contains a value that is not found in Str, then the explode () function returns an array containing a single element of STR, and if the limit parameter is a negative number, Then all elements except the last limit element are returned.
The sample code is as follows:

 
  

The results of the operation are as follows:

(2), use the implode () function to convert the array to a string.
The syntax format is as follows:
Implode (string glue, array arr)
The parameter glue is a string type that represents the delimiter to pass in. The parameter arr represents the name of the array variable passed in to merge the elements.
The sample code is as follows:

 
  

The results of the operation are as follows:

2, querying the specified element in the array
The Array_search () function, given the specified value in the array, returns the key name after it is found, otherwise returns false.
The syntax format is as follows:
Mixed Array_search (mixed Needle,array haystack [, BOOL strict])
The parameter needle specifies the value to be searched in the array, the parameter haystack specifies the array to be searched, the parameter strict is an optional parameter, and if true, the type of the given value is also checked.
The sample code is as follows:

 
  

The results of the operation are as follows:

3, array elements out of the stack
The elements in the array are arranged in order of the index values, so that the elements are like the elements of the stack after the stack, so getting the last element in the array is a bit like a stack.
The Array_pop () function gets the last element in the array and subtracts the length of the array by 1. If it is empty or not an array, it is returned as null.
The syntax format is as follows:
Mixed Array_pop (array arr)
Where the arr parameter is the name of the array variable name
The instance code is as follows:

 
  

The results of the operation are as follows:

4, adding elements to the array
The Array_push () function considers an array as a stack, pushes the passed variable to the end of the stack, the length of the array, increments the number of variables into the stack, and returns the number of new elements in the array.
The syntax format is as follows:
int Array_push (array arr, mixed var [, mixed ...])
The parameter arr is the specified array, VAR is the variable to be added
The sample code is as follows:

 
  

5. Delete duplicate elements in the array
The Array_unique () function removes duplicate elements from the array. By sorting the values as strings, and then preserving only one key name for each key value, ignoring all subsequent key names, the duplicate elements in the array are deleted.
The syntax format is as follows:
Array array_unique (array arr)
ARR is the variable name of the array variable to remove the repeating element

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.