PHP regular expressions, arrays, functions

Source: Internet
Author: User

<?php
Regular expressions
Slash stands for delimiters
/* $b = "ab2cde123fg1h";
$a = "/\d/";
Echo preg_replace ($a, "|", $b);//replace string B with regular expression a

Var_dump (Preg_split ($a, $b));//split B according to regular expression a
$shuzu = Array ();
Preg_match ($a, $b, $shuzu);//Match satisfies the first data in the regular expression
Var_dump ($shuzu);
Preg_match_all ($a, $b, $shuzu); *///matches All

Array
Defined
$a = array (1,2,3,4,5);//define Array mode 1
Assignment definition
$b [0] = 5;
$b [1] = 6;
Classification
1. Indexed arrays
$a = array (1,2,3,4,5);
2. Associative arrays
$b = Array ("One" =>5, "=>10");
echo $b ["one"];
PHP Array Features: Store any type of data, the length can be changed


$a 1 = array ("AA", "BB", "CC");
$a = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5,);
For loop traversal, cannot traverse associative array
/*for ($i =0; $i <count ($a); $i + +)
{
echo $a [$i]. " <br> ";} */

foreach traversal
1.foreach ($a as $value)
/*{
echo $value. " <br> ";}
*/

2.foreach ($a as $key = $value)
/*{
echo $key. " --". $value." <br> ";}
*/

3.each function
Var_dump (each ($a));//Returns the $key and $value inside the array, each pointing to the next

4.list () function
List ($a, $b, $c) = $a 1;//assigns the values inside the array to the variables in the list
echo $a;

5.each and List traversal arrays
/*while (List ($key, $value) =each ($a))
{
Each "($key)-($value) <br>";
}*/
6. Pointers
Var_dump (current ($a 1));//Take the value of the position pointed to by the present pointer
Echo key ($a 1);//key that points to the position of the current pointer
Next ($a 1);
Echo key ($a 1);//move the pointer downwards
Prev ($a 1);//Raise the pointer upward
End ($a 1);//Adjust the pointer to the last
Reset ($a 1);//Adjust the pointer to the first
/*do
{
echo key ($a). " --". current ($a);}
while (next ($a)) */
Print_r ();

In_array ("AA", $a);//Judging if there is no such value
Array_reverse ($a);//Flip An array
Count ($a);//Returns the number of array elements
Array_unique ($a); Array de-weight, output new array, index number problem
Array_values ($a);//re-index
Unset ();//delete the element in the array and delete the index number
Array_merge ($a, $a 1); Merging arrays
Array_push ($a, "DD"); Append elements to an array

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

Var_dump ($a); * *

/* $a = array (
Array ("n001", "Han"),
Array ("n002", "Miao"),
Array ("n003", "Hui"),
Array ("n004", "Uighur"),
);
Displays the contents of the array as a drop-down list
echo "<select>";
foreach ($a as $value)
{
echo "<option value= ' {$value [0]} ' > ($value [1]) </option>";}
echo "</select>"; * *

Display the contents of a two-dimensional array as a single table
$a = Array (
Array ("P001", "Zhang San", "Male", "Han"),
Array ("P002", ' John Doe ', ' Male ', "Miao"),
Array ("P003", ' Zhang Hong ', ' female ', "Hui"),
Array ("P004", ' Zhaowen ', ' female ', "Uighur"),
);
echo "<table width= ' 100% ' border= ' 1 ' cellpadding= ' 0 ' cellspacing= ' 0 ' >";
echo "<tr height= ' ><td> codes </td>><td> names </td>><td> gender </td>> <td> Han </td></tr> ";
echo "<tr><td>{$a [0][0]}</td><td>{$a [i][1]}</td><td>{$a [i][2]}</td> <td>{$a [i][3]}</td></tr> ";
for ($i =0; $i <count ($a); $i + +)
{
echo "<tr><td>{$a [$i][0]}</td><td>{$a [$i][1]}</td><td>{$a [$i][2]}</td ><td>{$a [$i][3]}</td></tr>];}

echo "</table>";


?>

PHP regular expressions, arrays, functions

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.