Intercept, divide, and replace partial arrays of PHP arrays

Source: Internet
Author: User
Tags random shuffle
intercept, divide, and replace partial arrays of PHP arrays

In this article we will describe the interception of arrays (Array_slice), the Array_chunk and substitution (Array_splice), and the difference between Array_slice and Array_splice!

In the previous three articles, "How do I sort php arrays?" "PHP array random Shuffle and reverse order" and "reverse Order of PHP array", we introduced the array of sorts, including the ascending, descending and disorderly order and reverse order of the array, I believe we have a certain understanding of the sorting of the group, Let's introduce another set of array functions today!

What is an intercept array? (Array_slice)

Array_slice an element from the array, the first parameter is the original array, the second parameter is the start subscript (remember that the array is starting from 0), the third parameter is the number of elements to be taken from the subscript, and if not set, the default is always taken to the end of the array!

The Array_slice syntax format is as follows:

Array array_slice (array $array, int $offset [, int $length = NULL [, bool $preserve _keys = false]])
Parameters Describe
Array The input array.
Offset If offset is non-negative, the sequence starts at this offset in the array. If offset is negative, the sequence starts at a distance from the end of the array.
Length If length is given and is positive, then there will be so many cells in the sequence. If length is given and is negative, the sequence terminates so far away from the end of the array. If omitted, the sequence starts at offset from the end of the array.
Preserve_keys Note Array_slice () is reordered by default and resets the numeric index of the array. You can change this behavior by setting Preserve_keys to TRUE.

Let's look at the following example code:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $arr =array (" topic.alibabacloud.com ",  " www.php.cn ",  " Baidu "," Sogou "); $array = Array_slice ($arr, ); Intercept from Subscript 1, take 2 elements, assign a new variable print_r ($array); >

The result of the output is:

The above example shows how many elements we specify to intercept, if we do not specify? What happens if the third parameter of Array_slice () is not written? Let's take a look at the sample code, or use the code above as an example:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $arr =array (" topic.alibabacloud.com ",  " www.php.cn ",  " Baidu "," Sogou "); $array = Array_slice ($arr, 1); Intercept from subscript 1, intercept to the last element, assign to the new variable Print_r ($array); >

The result of the output is:

See this everyone should understand that when the third parameter of Array_slice () is not written, then the last element of the array will be truncated by default! The second parameter of the Array_slice () function is to specify which subscript to start the Intercept from!

Here to illustrate:

Array_slice is simply to re-assign a paragraph in the array to another array, and the original array is unaffected, that is to say, the above code array of www.php.cn and Baidu still exist!

What is a replacement partial array? (Array_splice)

The Array_splice () function, like the Array_slice () function, selects a series of elements in an array, but does not return, but instead removes them and replaces them with other values, in other words, removing some part of the array and substituting other values.

The Array_splice syntax format is as follows:

Array Array_splice (array & $input, int $offset [, int $length = count ($input) [, Mixed $replacement = Array ()]])

Remove the cells specified by offset and length from the input array, and replace the units with the replacement parameter if provided.

Attention:

1. If the replacement is not an array, the type is converted to a group (for example: (array) $replacement). When an incoming replacement is an object or NULL, it causes an unknown behavior to occur.

2. Note that the numeric key name in input is not retained.

Let's look at the specific sample code:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $arr =array (" PHP ",  " language ",  " Baidu "," Sogou ");p Rint_r ($arr); echo" <br> "; Array_splice ($arr, 1,2,array ("topic.alibabacloud.com", "www.php.cn", "php.cn"));p Rint_r ($arr);? >

The result of the output is:

From the above examples can be seen in the array of "language", "Baidu" was "topic.alibabacloud.com", "www.php.cn", "php.cn" alternative to the area.

In the example above, we will replace and assign it to a new array, so what if there are no new arrays? Let's look at the following example:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $arr =array (" PHP ",  " language ",  " Baidu "," Sogou ");p Rint_r ($arr); echo" <br> "; Array_splice ($arr, ;p Rint_r ($arr);? >

The result of the output is:

From the output of the results can be seen, if there is no new array, then the equivalent of Array_slice, cut off the relevant position of the array!

Array_splice and Array_slice two functions of the same point and different points

Same point:

You can implement an array, specify the subscript position, and specify the number of elements to cut arrays

(In fact, it is to delete the specified array element)

Different points:

Array_slice is a function of value, the original array will not change, after cutting, you can assign to a new array!

Array_splice is the address function that modifies the original array directly, and you can set new elements to replace the cut-off array elements!

What is an array of arrays? (Array_chunk)

The Array_chunk () function cuts the number of elements in an array into a two-dimensional array, where the number of cells per array is determined by the second parameter, size. The number of the last cell in the array may be less than size, let's look at the syntax format together:

Below we directly use the example code to take you to understand:

<?phpheader ("content-type:text/html; Charset=utf-8 "); $input _array = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']; $new _array = Array_chunk ($input _array, 3);p Rint_r ($new _array);? >

The output is:

In the next article, we'll look at the functions of merging arrays, read more about how PHP arrays are merged. 》

"Related tutorials Recommended"

1. Related topics: "PHP array"

Related Article

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.