PHP regular && Array

Source: Internet
Author: User

<?php
Regular expressions
Slash stands for delimiter/^$/
$str = "abcde123fg456h";
$reg = "/\d/";
Preg_replace ($reg, "#", $str);//replace string str with regular expression reg
Var_dump (Preg_replace ($reg, "#", $str));


$str = "a1cd3fg6h";
$reg = "/\d/";
Var_dump (Preg_split ($reg, $STR))//split string according to regular expression


$str = "a1b2cd3e";
$reg = "/\d/";
$arr =array ();
Preg_match ($reg, $str, $arr);//Match the first data in a string that satisfies a regular expression


Var_dump ($arr);
Preg_match_all ($reg, $str, $arr);//Match all data in a string that satisfies a regular expression
Var_dump ($arr);

Array
$arr =array (1,2,3,4,5);//1. Define array mode
Var_dump ($arr);


2. Assignment definitions
$arr 1[0]=5;
$arr 1[1]=6;
$arr 1[2]=10;
Var_dump ($arr 1);


Array classification
1. Indexed arrays
$arr =array (1,2,3,4,5);
Echo $arr [2];

2. Associative arrays
$arr =array ("One" =>5, "one" =>10);
Echo $arr ["both"];
Var_dump ($arr ["the"]);

PHP Array Features: Can store any type of data, array length can be changed

Iterating through an array
$arr =array ("AA", "BB", 5, "CC", 10);

1.for loop traversal, cannot traverse associative array, traverse indexed array
for ($i =0; $i <count ($arr); $i + +)
{
echo $arr [$i]. " <br/> ";}



2.foreach traversal can traverse associative array
foreach (int a in $arr) C #
$arr =array ("A" =>10, "B" =>20, "c" = "Hello", "D" =>30);
foreach ($arr as $value)
{

echo $value. " <br/> ";

}

foreach ($arr as $key = $value)
{
echo $key. " --". $value."   <br/> ";} Traverse Key and value

3.each () function
$arr =array ("a" = "Hello", "b" = "Lily", "C" and "Love");
Var_dump (each ($arr));//returns the key and value of the element inside the array, first takes the first one, second takes the second one ... Take the value down one at a time.

Var_dump (each ($arr));
Var_dump (each ($arr));


4.list () function is a very special function, the function on the left
$arr =array (10,20, "Hello", 30);
List ($a, $b, $c, $d) = $arr;//Assign the values inside the array to the variables in the list function and only iterate through the indexed array
echo $a. " <br> ";
echo $b. " <br> ";
echo $c. " <br> ";
echo $d. " <br> ";


5.each () and list () iterate through the array.
$arr =array ("a" = "Hello", "b" = "Lily", "C" and "Love");

while (list ($key, $value) =each ($arr))
{
echo "{$key}--{$value}<br>";
}

6. Pointers
$arr =array ("a" = "Hello", "b" = "Lily", "C" and "Love");
Var_dump (current ($arr));//Take the value of the position of the present pointer
Next ($arr);//Adjust the pointer to one
Var_dump (Next ($arr));
echo Key ($arr);//The key that points to the position of the current pointer
Var_dump (Key ($arr));
Prev ($arr);//Move the pointer up one
Var_dump (prev ($arr));
End ($arr);//Adjust the pointer to the last
Var_dump (End ($arr));
Reset ($arr);//Reset the pointer
Var_dump (Reset ($arr));


$arr =array ("a" = "Hello", "b" = "Lily", "C" and "Love");
while (Next ($arr))
{
echo Key ($arr). " --". Current ($arr)." <br/> ";
//next is the beginning of the next index of 1, so a "=" and "Hello" will be omitted, so use Do{}while () to perform the condition first.


do{Echo Key ($arr). " --". Current ($arr)." <br/> ";} while (Next ($arr))

$_get $_post Store The value that the page passes over, method= "POST" in the form form

Var_dump ($_server);
Var_dump ($_post["UID"]);//Store the value of the variable passed over the page


Var_dump ($_get[]);
$arr =array ("AA", "BB", 5,5, "CC", 10,10);
Common functions
Var_dump (In_array ("AA", $arr));//Determine if there is a value in the array


Var_dump (Array_reverse ($arr));//Flip Array


echo count ($arr);//Returns the number of array elements


$attr =var_dump (Array_unique ($arr));//de-weight

unset ($arr [0]); To delete an element in an array


Var_dump ($arr);
$arr =array ("AA", "BB", 5,5, "CC", 10,10);
Var_dump (Array_unique ($arr));

$attr =array_unique ($arr);
Var_dump (Array_values ($attr)); Re-index


$attr = Array (1,2,3,4);
$arr =array ("AA", "BB", 5,5, "CC", 10,10);
Array_merge (SHUZU1,SHUZU2);//merge multiple arrays into an array


Array_push ($arr, "DD"); Append an element to an array

Var_dump ($arr);



Two-dimensional arrays
$arr =array (
Array (+/-),
Array (4,5,6),
Array (7,8,9));
Var_dump ($arr);


Example shows the form of a two-dimensional array as a drop-down list
$attr =array (
Array (' 001 ', ' Han '),
Array (' 002 ', ' Miao '),
Array (' 003 ', ' hui '),
Array (' 004 ', ' Uighur '),

);
echo "<select>";
foreach ($attr as $value)
{echo "<option value= ' {$value [0]} ' >{$value [1]}</option>";}
echo "</select>";


Display the contents of a two-dimensional array as a single table

$attr =array (
Array (' P001 ', ' Zhang San ', ' Male ', ' Han '),
Array (' P002 ', ' John Doe ', ' Male ', ' Miao '),
Array (' P003 ', ' Harry ', ' Male ', ' hui '),
Array (' p004 ', ' Zhao Liu ', ' female ', ' Uighur '),

);
echo "<table width= ' 100% ', heght= '", border= ' 1 ', cellpadding= ' 0 ', cellspacing= ' 0 ' > ";

echo "<tr><td> code </td> <td> name </td> <td> sex </td> <td> national </td> </tr> ";
for ($i =0; $i <count ($attr); $i + +)
{
echo "<tr> <td>{$attr [$i][0]}</td> <td>{$attr [$i][1]}</td> <td>{$attr [$i][2]} </td> <td>{$attr [$i][3]}</td> </tr>];
}

echo "</table>";

?>

<form action= "zhengze.php" method= "POST" >
<input type= "text" name= "UID"/>
<input type= "Submit", value= "Submission"/>

</form>
<?php
Var_dump ($_post["UID"]);
?>

PHP regular && 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.