Sinsing interpretation of key functions in PHP the array function of the first section

Source: Internet
Author: User

Here I have written its PDF version, more suitable for reading than this blog. First of all, it is in the Baidu network disk: Baidu network disk Download, if the left is connected to kneel, you can enter in the browser: Http://pan.baidu.com/s/1qW5AYzM to download. And then say it in Csdn, is free of points of Austria,: csdn resource Download, if the connection does not work, you can enter in the browser: http://download.csdn.net/detail/xinguimeng/7697309 to manually download.

Well, we know that PHP is still a lot of functions, it is these functions for PHP provides a strong support, this section we study the array function in PHP, because PHP is a weak type of language, its array is actually a bit like other programming language arrays, and a bit like other programming language dictionary, so, It's pretty flexible.

The first function is the array function, we usually use it to create an array it can be used to create a numeric array, associative array, multidimensional array, the function is also very powerful, it can enter all the values directly, you can also enter key value pairs, if it is a key value pair, multiple key-value pairs are separated by commas, and the key with = = to point to the value, or quite intuitive. In addition, if PHP version reaches 5.4, then you can use the syntax of the paragraph array to create, that is, we use [] instead of array (), the parentheses inside and the array function is written in the same thing.

The second function is Array_chunk, it is used for array segmentation, we can use it to divide the array into several elements of the array block, it accepts three parameters, the first parameter is an array name, the second parameter is a group of each element, the third parameter is optional, the default value is False, That is, the original key name is not preserved, and of course we can set it to true to preserve the original key name, and its output is a multidimensional array.

The third function is Array_column, which is useful, but it needs a PHP5.5 version, and here it is, it can take out the value of a column subscript in a multidimensional array, which is especially handy for array data taken from the database.

The fourth function is Array_combine, it is used to match the array of key-value pairs, it requires two parameters, the two parameters are two array names, the first for the key, the second for the value, they are common in the PHP5, and the array is very good use.

The fifth function is array_count_values to count the number of occurrences of an element, it only needs to enter an array name, it will automatically count the number of occurrences of the array of elements, and the return value is an array, the difference is that the value of the returned array will only be integers, and is a positive integer.

The sixth function is more complex, it is not a function, but a function family, is the Array_diff series, this series of array comparison function, Array_diff_assoc function comparison key name and key value, Array_diff_key function only compare key value, ARRAY_DIFF_UASSOC uses user-defined functions to compare key names and key values, Array_diff_ukey uses user-defined functions to compare key values. The result of these functions is a difference set, which can accept n parameters, which are the elements of the array from the array of the first parameter, and then return the remaining array.

The seventh function is popular Array_fill, it is available in the PHP4.2 version, it accepts three parameters, the first is the initial index, the second is the number of fill elements, the third is used to fill the array of key values, especially easy to understand.

The eighth function is a array_fill_keys, It is introduced in the PHP5.2 of a function, its first need to pass the argument is an array of keys, the second parameter is the value of the array, we get an array, its key is specified in the first parameter, the value is the second parameter used by our function, its name is called Array_ Fill_keys.

The Nineth function is Array_flip, which is to swap the key value pairs of the array, that is, the key as the value of the new array, the value as the key of the new array, the function is available in PHP4 or later.

The tenth function is array_filter, its role is to filter the array, the first parameter it needs is an array, the second is a callback function, when we call Array_filter, it will automatically pass the value to our callback function, After our callback function is judged, if the return value is true, it is preserved, otherwise, the element is discarded. It is worth noting that the second parameter of the array_filter here needs double quotation marks, forget it, I will give the code example here:

<?php function Xin ($var) {return ($var% 3 = = 0);} $arr = Array (4,5,6,7,8,9), $msg = Array_filter ($arr, "Xin");p Rint_r ($msg);

Then the output above will be an array, and the elements in the array are only 6 and 9.

This blog is written here on the first, Sinsing, look forward to your attention AO.

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.