PHP arrays and JS Array method collation

Source: Internet
Author: User
Tags array length

In the JS array very often used, proficiency in the array of methods can greatly improve the efficiency of their own, recently lifted PHP, learned very fur, encountered a problem, oneself has been trapped in the Foreach loop How to solve the dead loop. The result has not been able to solve very well, come back to ask a classmate, more think of some of the array of methods are very useful, so it is necessary to sort out some of the array of methods, I hope that they can remember them.

One, the JS array operation method:

Refer to the content: Http://www.jb51.net/w3school/js/jsref_obj_array.htm and the "JavaScript authoritative guide" and "jquery authoritative guide" to combine their usual work and study. Review the array operation method again.

1, sparse array (feel this usually should be very little use, so probably remember a concept is good): Sparse array is the array containing a discontinuous index starting from 0;

2, adding and removing array elements

Add: Push ();

Delete: Pop (); Deletes the last value of the array and returns the value. (Used with push ())

Delete: Shift (); Removes an element from the head of an array, which changes the length of the array by using this method, and drops all the remaining element indexes down to 1 lower than the current one. (Used with unshift ())

Delete: Delete (); using this method on an array does not change the length of the array (unlike shift ()), which causes the array to become a sparse array.

Multi-function Method: Splice () ; Insert to delete replacement array elements;

The splice () method deletes 0 or more elements starting at index and replaces the deleted elements with one or more values declared in the argument list.

If the element is removed from the Arrayobject, the array containing the deleted element is returned.

Instructions for how to use and how to do this

Arrayobject.splice (index,howmany,item1,....., ItemX)
Parameters Description
Index Necessary. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
Howmany Necessary. The number of items to delete. If set to 0, the item is not deleted.
Item1, ..., ItemX Optional. Adds a new item to the array.
type Description
Array A new array containing the deleted items, if any.

3,join () The method joins all the elements in the array into a string and returns a string. How to use: Array.join (delimiter)

4,reverse () Reverses the order of the elements in the array, returning the array after the reversed order.

5,sort () Sorts the elements in the array and returns them. When called with no arguments, the arrays are sorted alphabetically, with undefined in the array at the end.

6,concat () Creates and returns a new array whose elements include the elements of the array to which it is called and each parameter in the band, such as an array in the parameter that takes each value in the array. the called array is not modified

7,slice () returns the specified fragment or sub-array. Method of Use: Array.slice (start, end); the called array is not modified

8,splice () A common method of inserting or deleting elements in an array, modifying the array of calls

9,push () and Pop () add and delete one or more elements at the end of the array. and returns the array length.

Unshift () and Shift () add and delete one or more elements at the head of the array. Unshift () returns the array length, shift () returns the deleted element

One, toString () tolocalstring ()

Array methods in ES5

1,foreach () looping through an array

2,map () passes each element of the called array to the specified function and returns an array. Usage: a = [n/a]; b = A.map (function (x) {return x*x}); return b = [1,4,9]

3, filter () returns a subset of the call array. A = [n/a]; b = A.filter (function (x) {returnx<3}); return b = [+]. Is the meaning of filtering, using a function to filter the array.

4,every () and some () use the same method as filter, except that the filter returns a subset of the conditions that are eligible, and this method returns a Boolean value. Every (): Returns True when all elements satisfy the condition, otherwise returns False;some (): Returns True if an element satisfies, otherwise returns false. tips: When called in an empty array according to mathematical conventions, every returns True,some returns False

The 5,reduce () and reduceright () usages are similar to the filter, which returns the value of the function operation.

Second, the PHP array operation method:

In the PHP array operation method is also many, master these basic operation method, can give oneself peacetime turn to bring great convenience, after all, array is very common. Refer to W3c:http://www.w3school.com.cn/php/php_ref_array.asp to learn how to operate an array in PHP.

PHP arrays and JS Array method collation

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.