Php array (2)-php Tutorial

Source: Internet
Author: User
Php array (2) add elements to the array header
Array_unshift ($ states, 'AAA'); add aaa to the header of the array $ states, that is, the first

Add elements at the end of the array

Array_push ($ states, 'AAA'); add aaa to the end of the array $ states, that is, the last
Delete element from array header
Array_shift ($ states); deletes the first element of the array $ states
Deletes an element from the end of an array.
Array_pop ($ states); delete the last element of the array $ states
Returns true and false when searching for a specific value of an array.
In_array ($ states, 'AAA'); searches for the $ states array to check whether the value aaa exists. if the value aaa exists, true is returned.
Returns true and false when searching for the key of the associated array.
Array_key_exists ('AAA', $ states) returns true if the key in the array $ states contains an aaa key.
Search for the value of the associated array
Array_search ('AAA', $ states) returns true if the value of $ states in the array contains the value aaa. this is a bit like in_array, but this is dedicated to searching associated arrays, the latter is to search all arrays, including normal arrays
Obtain the array key
$ Keys = array_keys ($ states); the keys of all the arrays are returned and arranged as normal arrays, such as Array ([0] => aaa, [1] => bbb)
Get array value
$ Values = array_values ($ states); similar to the above.
Get the current array key
$ Test = array ('AAA' => 'we', 'BBB '=> 'you'); while ($ key = key ($ test )) {// The key () is the key used to obtain the current array. The key is queried sequentially from 0. Therefore, the first key is aaa, and the second key is bbb echo $ key; next ($ test); // because the key does not move the array pointer and points to a key, it does not change. Therefore, you need to use the next method to move to the next pointer}
Get the current array value

Similar to the above, but the key is changed to current

Obtains the keys and values of the current array.

Similar to the above, use each

Move an array pointer

There are "next", "prev", "reset", and "end", which are respectively the next, first, first, and last.

Refer to the previous next example.

Determine the array size
count($garden);
Count the frequency of array keys
Array_count_values ($ states); return value: Array ([aaa] => 2, [bbb] => 1)
Determine unique array elements

Deletes all repeated values in the array and returns an array consisting of unique values.

array_unique($states);

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.