Php core technology-array use (1)

Source: Internet
Author: User
Because there are many elements in a number Group, when we locate the elements of an array, we usually use the form of the underlying method of the elements of the array. But sometimes, especially when traversing every element in the array (in a set, processing cyclically), we use the element positioning method as an array pointer. At the same time, this pointer can only point to an array element array: a data type. Set type. A set of data of the same type. An ordered set of key-value pairs. Key/value pairs. An array is composed of elements: an element is a set of key-value pairs. Key element subscript value element value. Traversal: process all array elements from start to end. How to locate array elements: because there are many elements in a number Group, when we locate the elements of an array, we usually use the following method of array elements. But sometimes, especially when traversing every element in the array (in a set, processing cyclically), we use the element positioning method as an array pointer. At the same time point, this pointer can only point to an array element. How to obtain an array? How to define an array? Array (); arrayname [] = value; generally, when defining an Array element, we must specify the subscript and value for the element. The subscript of the array element, which must be an integer or a string type. The value of an array element can be of any data type. Subscript: it is usually an integer or a string type. However, if you accidentally set a subscript of another type for the array element, php will try to convert the subscript of another type into a reasonable form. However, arrays and objects cannot be used as subscripts. If no subscript is specified when defining an array element, it means we need to use an integer subscript. The base object size depends on the current array structure. The new subscript is determined based on the maximum integer value in the current array, in the form of + 1. If no integer value exists in the current array, it starts from 0. If we need to define an array whose subscript starts from 1, how should we define it? If the underlying definition is a string that can be directly converted to an integer, the string will be converted to an integer: opposite to it. if it cannot be directly converted to an integer, the string type is used. You can use an empty string as the subscript. negative numbers can be used as the subscript, but this negative number is not used as the basis for generating the lower mark (not + 1 on the negative number). Tip: the above test is also applicable to the [] syntax, that is, different data types, and can be written in brackets. [] In brackets, the subscript of the array element should be used to locate the element. [Expression] can be written into an expression or a constant: but when we use a string as the lower mark, we should add quotation marks around the string. Otherwise, the identifiers will be parsed into constants first, if no constant exists, it is considered as a string. Will reduce the efficiency. Even when a constant exists, the business logic may have a problem: Tip: the above problem occurs not because of [] Parsing, but because php does this when parsing constants. In Php, the element value can be of any data type or array type. When the element value is of the array type, it can be called a multi-dimensional array. However, there is no multi-dimensional array in nature. In the preceding multi-dimensional array, the value of an array element is an array. If it is a multi-dimensional array, you can use multiple [] to access the value of an element: array classification: divided by the index type of the array element: index array and associated array. Index array: the subscript of an element is an array of values. it is a series of array elements starting from 0. Join array: the subscript of an element is correlated with the value of an element. the subscript of an element is a string type and can be used to describe the information of a string. However, due to the particularity of the php array, the subscript of the php array element can be both a numerical index or a string Association, and can exist at the same time. Because the php array is implemented by a linked list (hash table) in the data structure, it is a key-value pair. Because the array contains multiple elements, we usually use the value of an element, so we will always encounter traversing the array. Array traversal: The array variables basically depend on the array pointer. When we create an array, the internal pointer of the array points to the first element of the array. If we can get the information of the elements pointed to by the array pointer every time. Then we can move the array pointer from start to end to complete array traversal. The most common method to traverse arrays is the foreach language structure:


Foreach (the array to be traversed as to save the value variable of the element pointed to by the current array pointer) {loop body. Here we can process the elements of the array currently traversed}


Foreach (the array to be traversed as saves the subscript variable of the element pointed to by the current array pointer => saves the value variable of the element pointed to by the current array pointer) {loop body. You can process the array elements Currently traversed here.} Note: in the code above, we did not display the operations for moving the array pointer, however, foreach moves the current pointer down one bit after each element information is obtained. After the traversal is complete, foreach moves the array pointer to the beginning of the array. It can ensure that an array can be foreach infinitely. How should I traverse multi-dimensional arrays? You can nest foreach in foreach again to achieve the effect: Foreach's transfer method: value transfer: However, compared with the first one, value transfer also supports reference transfer: If you need to modify the original array, you can also directly operate the original array:

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.