_php Tutorial on the summary of array functions in PHP5

Source: Internet
Author: User
I do not know PHPHow does a beginner Learningfunction, but I think that learning he must be purposeful, it is a goal-driven, with array functions as an example! How do we learn so many array functions? In fact, we just need to know what we need to do with the array, and then classify these operations, the mind has a general impression, and then with your practice, these functions you are very clear, here to remind everyone is the manual to be standing! Needless to say, array functions generally fall into the following categories (see English Don't Be afraid OH):
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 ()
I don't want to talk about it if I'm not familiar with it! The most basic function, of course, is to output the array is also using the loop structure output, but there is such a convenient output function why not do it!

Creating arrays
Array ()
This function I do not want to say, this is actually only a language structure, everyone in the new array when the use of, no use I can only say, brother, I have no words!
List ()
This function, like the above mentioned array (), is just a language structure that assigns values to a set of variables in one step!
See the Manual for specific examples!
Range ();
Array Range(Mixed low, mixed high[, number step])
Create an array containing the specified range of cells
Example: $arr =range (0, 6);
Equivalent to $arr =array (0, 1, 2, 3, 4, 5, 6);
The general data is arranged from low to high, if low> high, it is arranged from highest to lowest;

Testing for an array
Is_array ();
Test the variable if the array type is true, not return false, very basic, very useful function!

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

copy PHP content to clipboard

PHP Code:

$states =array(‘Ohio’, ‘New York’);
array_push($states, ‘California’,‘Texas’);
//output: array((‘Ohio’,‘New York’ , ‘California’,‘Texas’);

Array_pop ();
pops up and returns the last cell of the array, reducing the length of the array by one. The array pointer is reset after use!
Example:

copy PHP content to clipboard

PHP Code:

$states =array(‘Ohio’, ‘New York’);
$state = array_pop($states);
//output: New York

Array_shift ();
Similar to Array_pop, except that it moves out and returns the first value of the array. The numeric key will start counting again from zero, and the text key name stays the same!

Array_unshift ();
Similar to Array_push, just inserting data from the head of an array! The numeric key will start counting again from zero, and the text key name stays the same!

Array_pad ();
ArrayArray_pad(array input, intpad_size, mixed pad_value)
Fills the array to the specified length with a value!
Pad_size is positive, then from the right start filling, negative, starting from the left to fill, small with the array length, then keep the array unchanged! See the Manual for examples!

Locating array elements
In_array ();
Check if there is a value in the array, base, don't say, see Manual for details!

Array_keys ();
ArrayArray_keys(array input [, mixed search_value [, BOOL strict]])
Returns all the 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 to compare (= = =).

Array_key_exists ();
boolarray_key_exists(Mixed Key,array search)
Checks whether the given key name or index exists in the array, and returns true if it exists. Some more specific applications remember to check out the manuals!

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

Array_search ();
MixedArray_search(Mixed Needle,array haystack [, BOOL strict])
Searches for the needle parameter in haystack and returns the key name if found, otherwise returnsFALSE
If needle is a string, the comparison is case-sensitive!
If strict is true, you have to compare the type
If needle appears more than once in haystack, the first matching key is returned. To return all keys that match a value, you should use Array_keys () plus optional parameter search_value instead!

http://www.bkjia.com/PHPjc/486501.html www.bkjia.com true http://www.bkjia.com/PHPjc/486501.html techarticle I do not know how PHP beginners learn the function, but I think that learning he must be purposeful, it is a goal-driven, with array functions as an example! So many array functions we ...

  • 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.