Summary of array functions in PHP5

Source: Internet
Author: User

I don't know how PHP beginners learn function numbers, but I think it must be purposeful. It is objective-driven. Take array functions as an example! How can we learn so many array functions? In fact, we only need to know what operations we need to perform on the array, and then classify these operations, so we have a general impression in our hearts. Then, with your practice, you will be very clear about these functions. Here, I would like to remind you that the manual should be retained! Not to mention, array functions are generally classified into the following categories (do not be afraid of English ):
Outputting arrays
Creating arrays
Testing for an array
Locating array elements
Traversing arrays
Determining array size and element uniqueness
Sorting arrays
Merging, slicing, splicing, and dissecting arrays

Outputting arrays
Print_r ()
If I am not familiar with this, I don't want to say more! The most basic function, of course, Outputs An array but uses a loop structure for output. But why not use this convenient output function!

Creating arrays
Array ()
I don't want to talk about this function either. In fact, this function can only be regarded as a language structure. You have used it when creating arrays. If you have never used it, You can only say it, bro, I'm speechless!
List ()
This function is just a language structure like the array () mentioned above. It assigns values to a group of variables in one step!
For more information, see the manual!
Range ();
ArrayRange(Mixed low, mixed high [, number step])
Creates an array containing a specified range of units.
Example: $ arr = range (0, 6 );
Equivalent to $ arr = array (0, 1, 2, 3, 4, 5, 6 );
Generally, data is arranged from low to high. If low> high, data is arranged from high to low;

Testing for an array
Is_array ();
Test whether the variable is of the array type. If yes, true is returned. If not, false is returned. It is a basic and practical function!

Adding and removing array elements
Array_push ()
IntArray_push(Array & target_array, mixed var [, mixed...])
Add one or more cells to the array from the end! Returns the total number of cells in the new array!
Example:

Copy the PHP content to the clipboard. PHP code: $states =array(‘Ohio’, ‘New York’);
array_push($states, ‘California’,‘Texas’);
//output: array((‘Ohio’,‘New York’ , ‘California’,‘Texas’);

Array_pop ();
The last cell of the array is displayed and returned, and the length of the array is reduced by one. The array pointer is reset after use!
Example:

Copy the PHP content to the clipboard. PHP code: $states =array(‘Ohio’, ‘New York’);
$state = array_pop($states);
//output: New York

Array_shift ();
It is similar to array_pop, except that it removes and returns the first value of the array. The number key will record data from scratch, and the text key name will remain unchanged!

Array_unshift ();
Similar to array_push, data is inserted from the array header! The number key will record data from scratch, and the text key name will remain unchanged!

Array_pad ();
ArrayArray_pad(Array input, intpad_size, mixed pad_value)
Fill the array with the specified length!
If pad_size is positive, it is filled from the right, negative, left, small, and array length! For examples, see the manual!

Locating array elements
In_array ();
Check whether a value exists in the array. For more information, see the manual!

Array_keys ();
ArrayArray_keys(Array input [, mixed search_value [, bool strict])
Returns all key names in the array. If search_value is selected, the selected key name is returned! From PHP 5], you can use the strict parameter for full comparison (= ).

Array_key_exists ();
BoolArray_key_exists(Mixed key, array search)
Check whether the given key name or index exists in the array. If yes, true is returned. For more specific applications, see the manual!

Array_values ();
ArrayArray_values(Array input)
Similar to array_keys! All key values of the array are returned!

Array_search ();
MixedArray_search(Mixed needle, array haystack [, bool strict])
Search for the needle parameter in haystack and return the key name if it is found. OtherwiseFALSE.
If needle is a string, it is case sensitive!
If strict is true, the comparison type is required.
If needle appears more than once in haystack, the first matched key is returned. To return keys of all matched values, replace array_keys () with the optional parameter search_value!

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.