PHP Core technology-use of arrays

Source: Internet
Author: User
Tags foreach define array arrays end expression integer variable

 

Array: is a data type. Collection type. A set of data with the same type. A   ordered set of   key-value pairs. key/value  key value pairs. Arrays are made up of    element  : An element is a set of key-value pairs. where   key   element subscript   Value   element   value. Traversal: All of the array elements are processed from beginning to end   sequentially. How to locate an array element: because there are many elements in an array  , usually when we position the elements of an array, we take the form of the   subscript of the elements of the array. But sometimes, especially when traversing (centralizing, iterating over every element within an array), we use the element positioning method as a   array pointer.   At the same time, this pointer can only point to an array element. How do I get an array? How do I define an array?         Array ();     arrayname[]= values; In general, when we define the elements of an array, we need to specify the subscript, and the value, for the element. Where the subscript of an array element requires an integer   or a string type. The value of an array element can be any data type.   Subscript: are usually integral and string-type. However, if an unexpected array element has a different type of subscript set, PHP will try to convert other types of subscripts into a reasonable form. However, you cannot use the   array and object   as subscript.   If you do not make a subscript when defining an array element, it means   we need to use an integer subscript. The size of the subscript is determined by the current array structure. The new subscript is determined according to the value of the largest integer under the current array, in the form of  +1. If the current array does not have an integer value, it starts at 0 . What should we define if we need to define an array with the subscript starting at 1?   If the definition of subscript   is a string that can be directly converted to a   integer, the string is converted to an integral type:   is relative to it and continues to use the string type if it cannot be converted directly to an integral type.   can use   empty string as subscript: You can use   negative numbers as subscript, but you will not use this negative number as the basis for generating subscripts (not on a negative basis +1);   TIP: The above test   also applies to  [  Syntax, which is a different data type, can be written in brackets. Inside brackets [], you should use the subscript of an array element to locate the element.   Expressions can write expressions     can also write constants: But   when we use strings as subscripts,,  should add quotes around the strings, otherwise the identifiers are first parsed into constants, if there are no corresponding constants, will be considered a string. will reduce efficiency. Even in the presence of the corresponding constants, the business logic is problematic:   TIP: The reason for the above problem   is not due to [] parsing, but to PHP when parsing the constants  .     PHP Because the value of an element can be any data type, it can also be an array type. When the value of an element is an array type, we can call it a multidimensional array. However, there is no multidimensional array in nature. The multidimensional array mentioned above is just a list of the values of a certain number of elements. Similarly   if it is a multidimensional array, you can use multiple [] to access the value of an element:     Array Classification: According to the index type of the array elements: indexed array and associative array. Indexed arrays: The subscript   of an element is an array of numeric values,  typically   a series of array elements starting with 0. Associative arrays: The subscript of an element is associated with the value of the element, the element subscript is a string type, and the string can have the ability to describe the information.     However, because of the specificity of the PHP array, the subscript   of the PHP array element can be both numeric and string associative.     Because the PHP array, on the data structure   is a linked list (hash table) implemented, is the key value pairs. Because the array contains more than one element, we usually use the value of one of the elements, so we always encounter the traversal array.   Traversal of arrays: Array variables are basically dependent on the array's pointer completion. When we just set up an array, the pointer inside the array is the first element of the array. If we can get information about the elements that the   array pointer points to. Then we can complete the array traversal by moving the array pointer from the beginning to the end. Our most common method of traversing arrays   is the  foreach  language structure:

Foreach (the array that needs to be traversed as the value variable that holds the element to which the current array pointer is pointing) {The loop body. Here you can handle the array elements that are currently being traversed

Foreach (the array to be traversed as the subscript variable => the element to which the current array pointer points) saves the value variable of the element that the current array pointer points to) {The Loop body. Here you can handle the array elements that are currently being traversed. Note: In the above code, we do not display the action to perform the move array pointer, but foreach moves the current pointer down one bit after each time the element information is obtained. And after the traversal is complete, foreach moves the array pointer to the beginning of the array. An array can be guaranteed to be an infinite number of foreach.   If it is a multidimensional array, how should you traverse? You can nest foreach again in foreach to achieve the effect: foreach passed by: Value delivery: But with the first, the value pass is also supported by the reference pass: If you need to modify the original array, you can also directly manipulate the form of the original array: String functions: A small number of ordinary characters The processing of strings, usually by using the string processing function to complete the processing, in bytes. GBK encodes a Chinese character for 2 bytes Utf8 encodes a Chinese character for 3 bytes. Strlen (); Length Substr (string, starting position, intercept length): The Intercept string is processed according to the character. For example, a JavaScript string is processed by character. PHP does not use character processing by default, and it needs to be implemented using PHP's multibyte character extensions. PHP.ini: Mb_strlen ();   Mb_substr (); Regular expression batch string processing, regular expression. Verify that the data entered by the user is a mailbox? Whether to conform to a string rule. Common use of regular places: Form validation-Validation of user data (data from browsers, Get,post). Regular and PHP regular joint processing that uses JavaScript. Processing of acquisition-reptiles:

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.