PHP array-array regular expression, array, pre-defined array, array Regular Expression

Source: Internet
Author: User
Tags array example

PHP array-array regular expression, array, pre-defined array, array Regular Expression

Regular Expression

1. Replace

 

$ S = "hello5world"; $ s = preg_replace ("/\ d/", "#", $ s); echo $ s; // The output result is hello # world, the number is replaced #. // The first digit is the replaced content. "/\ d/" indicates the number of the regular expression. // What is the second parameter to be replaced. // The third parameter is the array to be operated.

 

2. Split

// $ S = "hello5world"; // $ attr = preg_split ("/\ d/", $ s ); // split by regular expression // var_dump ($ arr); // The first parameter is based on what to split, and the second parameter is the string to be split.

3. Match

// $ S = "he8alo5wor6ld"; // $ arr = array (); // preg_match ("/\ d/", $ s, $ arr ); // match the first string that matches the regular expression. In this case, 8 (8) is the first number in the string. // Preg_match_all ("// \ d/", $ s, $ arr); // match all strings that meet the regular expression, all numbers (8, 5, and 6) are matched ). // What is the first parameter match? // The second parameter is the matching array. // The third parameter is another array or an empty array, the array after matching is placed in this array.

Array

1. traverse the Array

(1) for Loop traversal, only index arrays can be traversed

// $ Attr = array ("aa", "bb", "cc", "dd ");

/* For ($ I = 0; $ I <count ($ attr); $ I ++)

{Echo $ attr [$ I]. "<br> ";}*/

(2) foreach traversal. The association and index can be traversed.

 

// $ Attr = array ("aa", "bb", "cc", "dd");/* foreach ($ attr as $ v) {echo $ v. "<br>";} * // only the retrieved value can be traversed./* foreach ($ attr as $ k => $ v) {echo $ k. "--". $ v. "<br>";} * // You can traverse the key and value $ attr = array (, 5 ); foreach ($ attr as $ k => $ v) {if ($ v = 3) {unset ($ attr [$ k]); // delete an element or a variable. You cannot directly unset ($ v) because the number 3 in the array is copied to $ v and the number 3 in the array is deleted, instead of in the array. } $ Attr = array_values ($ attr); // re-index. After 3 in the array element is deleted, the index value changes to 0, 1, 3, 4. after the index is re-indexed, it becomes 0, 1, 2, and 3. Var_dump ($ attr); // when using foreach to traverse elements in an array in other languages, you cannot delete or add elements in foreach. Only php can be used.

 

Predefined Array

1. $ GLOBALS references global variables in the global scope

Var_dump $ GLOBALS () // $ GLOBALS is an array customized by the system. It cannot be modified and must be capitalized. An array representing global variables.

2. $ _ SERVER is the SERVER information

3. $ _ ENV indicates the environment variable

4. $ _ GET indicates the data transmitted by the user in the _ GET mode.

5. $ _ POST indicates the data transmitted by the user using the post method.

The method must correspond to the obtained method. Var_dump ($ _ POST)

6. $ _ FILES; is about the form submission file.

<Form action = "text. php" method = "post" enctype = "multipart/form-date"> // when submitting a file, you must enter the enctype attribute. post is used for submission.

7. $ _ SESSION

Session control: a mechanism for saving user login information. If you have not logged on to the previous page for a certain period of time, you will be asked to log on again after refreshing the page. This is the effect of _ SESSION. It has a fixed time, And the logon information will be cleared later. _ SESSION stores logon information on the server. _ SESSION is more secure.

8. $ _ COOKIE

Session control: a mechanism for saving user login information. If you have not logged on to the previous page for a certain period of time, you do not need to log on again after refreshing the page. This is the effect of _ COOKIE. _ COOKIE storage login information is stored on the hard disk. _ COOKIE is not as secure as _ SESSION.

Other common functions 1. in_array (,); determines whether a value exists in this array. There are two parameters in the brackets. The first parameter is the existing value, and the second parameter is the checked array. 2. array_reverse (); flip function, which contains the array to be flipped. 3. array_unique (); Delete duplicate values and remove duplicates. 4. unset ($ attr [0]); delete a value in the array. 5. array_values (); Re-index. It is generally used together with deletion. 6. array_merge (); merge the array. Fill in n arrays to be merged in brackets. 7. array_push (); add a value to the array and add the position at the end.

Array example

1. Take the content in the array as a drop-down list
Method 1: (embed the PHP code) $ attr = array (// defines an array and uses a two-dimensional array. Array ("n001", "Han"), array ("n002", "Hui"), array ("n003", "Manchu"), array ("n004 ", "Uygur"); <select> <option> all </option> <? Php/* foreach ($ attr as $ v) {echo "<option >{$ v [1] }</option>" ;}*/?> </Select> Method 2: (PHP code does not need to be embedded) $ attr = array (// defines an array and uses a two-dimensional array. Array ("n001", "Han"), array ("n002", "Hui"), array ("n003", "Manchu"), array ("n004 ", "Uygur"); echo "<select>"; echo "<option> all </option>"; foreach ($ attr as $ v) {echo "<option >{$ v [1] }</option>" ;}echo "</select> ";

2. traverse the two-dimensional array to the table

$ Attr = array ("code", "name", "ethnicity", "Age"), array ("001", "Zhang San", "Han ", "23"), array ("002", "Li Si", "Hui", "34"), array ("003", "Wang Wu", "Uygur ", "53"), array ("004", "", "Zhuang", "26"); echo "<table border = '1px '> "; for ($ I = 0; $ I <count ($ attr); $ I ++) {echo "<tr>"; for ($ j = 0; $ j <count ($ attr [$ I]); $ j ++) {echo "<td> ". $ attr [$ I] [$ j]. "</td>";} echo "</tr>";} echo "</table> ";

 

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.