69
1 Using Array array คคคคคคคคคคคคคคคคคคคคคคค คคคคคคคคค คคคคคคคคคคคคคคคค คคคคคคค คคคคคคคค คคคค array คคคคคคคคคค คคคคคคคคคคคคคคคคค (element) คคคค index $ar_ID[0] = 13900990; $ar_ID[1] = 13901032;

PHP Tutorial (array)

  • Upload
    -

  • View
    1.225

  • Download
    2

Embed Size (px)

Citation preview

Page 1: PHP Tutorial (array)

1

Using Array array คื�อชุ�ดข้�อมูลที่ �ถูกนำ��มู�เรี ยงต่�อๆ ก�นำอย��งมู

ล��ด�บ โดยเก�บ ในำต่�วแปรี ชุนำ$ด array ส�มู�รีถูอ��งถู&งสมู�ชุ$กแต่�ละ

ต่�ว (element)

ด�วย index $ar_ID[0] = 13900990;

$ar_ID[1] = 13901032;

Page 2: PHP Tutorial (array)

2

Using Array ฟั)งก*ชุ� �นำ foreach() ใชุ�ด&งข้�อมูลโดยไมู�อ��งถู&ง index ข้อง

ต่�วแปรี array ออกมู�ที่��ง�นำเป,นำรีอบ โดยไมู�ใชุ� loop

รีปแบบที่ � 1

foreach(array_expression as $value){

statement;

}

รีปแบบที่ � 2

foreach(array_expression as $key => $value){

statement;

}

Page 3: PHP Tutorial (array)

3

Using Array สรี��ง array ด�วยฟั)งก*ชุ� �นำ array()

Demo <? #array01.php

$ar_name = array(“ที่$นำกรี",“มู�นำพ", “กว$นำ");

foreach($ar_name as $eachname)

{

echo $eachname.”<br>”;

}

?>

Page 4: PHP Tutorial (array)

4

Using Array สรี��ง array ด�วยก�รีก��หนำดคื��ให�ก�บต่�วแปรี array

Demo <? #array02.php

$ar_ID[] = 13900990;$ar_ID[] = 13901032;$ar_ID[] = 13900854;foreach($ar_ID as $i => $Emp_ID)

{print(($i+1)."). ".$Emp_ID."<br>");

} ?>

Page 5: PHP Tutorial (array)

5

Using Array สรี��ง array ด�วยก�รีก��หนำดคื��ให�ก�บต่�วแปรี array

Demo <? #array03.php

$country[] = "China";

$country[] = "Thailand";

$country[] = "India";

$country[] = "Japan";

print "<select name=selCountry>";

foreach($country as $countryCode => $countryName){

print("<option value=$countryCode>$countryName</option>");

//print $countryName."<br>";

}

print "</select>";

?>

Page 6: PHP Tutorial (array)

6

Using Array สรี��ง array ด�วยก�รีก��หนำดคื��ให�ก�บต่�วแปรี array

Demo <? #array05.php

$country = array("CH" => "China",

"TH" => "Thailand",

"IN" => "India",

"JP" => "Japan");

print "<select name=selCountry>";

foreach($country as $countryCode => $countryName){

print("<option value=$countryCode>$countryName</option>");

//print $countryName."<br>";

}

print "</select>";

?>

Page 7: PHP Tutorial (array)

7

Using Array สรี��ง array ด�วยฟั)งก*ชุ� �นำ range()

ใชุ�ในำก�รีสรี��ง array ที่ �มู คื��เป,นำล��ด�บจำ��นำวนำเต่�มู เชุ�นำ array(1,2,3,4,5) หรี�อ เป,นำล��ด�บข้องต่�วอ�กษรีต่�วเด ยว เชุ�นำ

array(“a”, “b”, “c”, “d”, “e”)

Demo

<? #array04.php

$ar_char=range(“A",“Z");

foreach($ar_char as $ar_value){

print("- ".$ar_value."<br>");

}

?>

Page 8: PHP Tutorial (array)

8

Using Array ฟั)งก*ชุ�นำ array_unique() ใชุ�ก��จำ�ดข้�อมูลในำ array

ที่ �มู คื��ซ้ำ�2�ก�นำDemo

<? #arunique.php

$arFruit = array("apple","mango","guava","apple","strawberry");$aruFruit = array_unique($arFruit);

print "Fruit in arFruit[] <br>";foreach($arFruit as $index => $fruit){

print($index+1)." - $fruit<br>";}print "Fruit in aruFruit[] <br>";foreach($aruFruit as $index => $fruit){

print($index+1)." - $fruit<br>";}

?>

Page 9: PHP Tutorial (array)

9

Using Array ก�รีเรี ยงล��ด�บข้�อมูลในำ array ด�วยฟั)งก*ชุ�นำ

sort(), rsort() เรี ยงล��ด�บข้�อมูลจำ�กนำ�อยไปห�มู�ก (ascending) แบบ case

sensitive ใชุ� sort()

เรี ยงล��ด�บข้�อมูลจำ�กมู�กไปห�นำ�อย (descending) แบบ case sensitive

ใชุ� rsort()Demo

<? #sort.php$arFruit = array("apple","mango","guava","apple","strawberry");$aruFruit = array_unique($arFruit);sort($aruFruit);print "Fruit in aruFruit[] - sort asc.<br>";foreach($aruFruit as $index => $fruit){

print($index+1)." - $fruit<br>";}

?>

Page 10: PHP Tutorial (array)

10

Using Array ก�รีเรี ยงล��ด�บข้�อมูลในำ array ด�วยฟั)งก*ชุ�นำ

asort(), arsort() เรี ยงล��ด�บข้�อมูลจำ�กนำ�อยไปห�มู�ก (ascending) เหมู�อนำ sort()

เรี ยงล��ด�บข้�อมูลจำ�กมู�กไปห�นำ�อย (descending) เหมู�อนำ rsort()

แต่�ไมู�จำ�ดเรี ยงคื ย*ใหมู�Demo

<? #asort.php$arFruit = array("apple","mango","guava","apple","strawberry");$aruFruit = array_unique($arFruit);asort($aruFruit);print "Fruit in aruFruit[] sort asc.<br>";foreach($aruFruit as $index => $fruit){

print($index+1)." - $fruit<br>";}

?>

Page 11: PHP Tutorial (array)

11

Using Array ก�รีเรี ยงล��ด�บข้�อมูลในำ array ด�วยฟั)งก*ชุ�นำ

natsort() ห�กข้�อมูลที่ �ต่�องก�รีเรี ยงล��ด�บหนำ��ต่�เหมู�อนำก�นำแต่�ลงที่��ยด�วยต่�วเลข้ที่ �ส�มู�รีถูจำ�ดล��ด�บ

ได�จำะพบป)ญห�ว��ก�รีเรี ยงล��ด�บเป,นำด�งนำ 2 1 10 2 20 natural sort หรี�อ natsort() ส�มู�รีถูแก�ป)ญห�ได�แบบ case sensitive

Demo <?php #natsort.php $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png"); echo "Start data\n"; print_r($array1);

print "<br>";

sort($array1); echo "Standard sorting\n"; print_r($array1);

print "<br>"; natsort($array2); echo "\nNatural order sorting\n"; print_r($array2); ?>

Page 12: PHP Tutorial (array)

12

Using Array ก�รีเรี ยงล��ด�บข้�อมูลในำ array ด�วยฟั)งก*ชุ�นำ

natcasesort() ชุ�วยในำก�รีเรี ยงล��ด�บข้�อมูลเหมู�อนำ natsort() แต่�เป,นำแบบ case insensitive

Demo <?php #natcasesort.php

$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

echo "Start data\n";print_r($array1);print "<br>";

sort($array1);echo "Standard sorting\n";print_r($array1);print "<br>";

natcasesort($array2);echo "\nNatural order sorting (case-insensitive)\n";print_r($array2);

?>

Page 13: PHP Tutorial (array)

13

Using Array ก�รีเรี ยงล��ด�บข้�อมูลในำ array ด�วยฟั)งก*ชุ�นำ

ksort(), krsort() ชุ�วยในำก�รีเรี ยงล��ด�บข้�อมูลโดยคืงคืว�มูส�มูพ�นำธ์*รีะหว��งคื ย*ก�บข้�อมูล (ต่�มู index)

Demo

<?php #ksort.php$city = array("Thailand" => "Bangkok",

"Japan" => "Tokyo","Canada" => "Ottawa","China" => "Beijing","India" => "Dehli");

ksort($city);foreach($city as $country => $capital){

print $country." - $capital<br>";}

?>

Page 14: PHP Tutorial (array)

14

Using Array ก�รีเรี ยงล��ด�บข้�อมูลในำ array ด�วยฟั)งก*ชุ�นำ

array_reverse() ชุ�วยในำก�รีเรี ยงล��ด�บข้�อมูลในำ array ใหมู�โดยคื�� element ส�ดที่��ยจำะข้&2นำมู�เป,นำ

คื��แรีก และคื��แรีก กล�บมู�เป,นำคื��ส�ดที่��ย

Demo

<?php #arreverse.php$city = array("Thailand" => "Bangkok",

"Japan" => "Tokyo","Canada" => "Ottawa","China" => "Beijing","India" => "Dehli");

$result = array_reverse($city);foreach($result as $country => $capital){

print $country." - $capital<br>";}

?>

Page 15: PHP Tutorial (array)

15

Using Array ก�รีรีวมู array เข้��ด�วยก�นำ (array merging)

ใชุ� operator + ในำก�รีรีวมู array 2 ชุ�ด เข้��ด�วยก�นำ ห�กคื��ในำ array ที่�2งสองที่ �นำ��มู�รีวมูก�นำมู คี�ย์�” ” ที่ �ซ้ำ�2�

ก�นำ element

นำ�2นำจำะไมู�ถูกรีวมูมู�ด�วย (element ของอาร์�เร์ที่��นำ�ามาร์วมที่�หลั�ง)

Page 16: PHP Tutorial (array)

16

Using Array ก�รีรีวมู array เข้��ด�วยก�นำ (array merging)

ใชุ� operator +Demo

<? #operator.php$oldcountry = array("CH" => "Chaina",

"TH" => "Thailand", "IN" => "India",

"JP" => "Japan");$newcountry["CH"] = "Chiangmai";$newcountry["TH"] = "Thibet";$newcountry["HK"] = "Hong Kong";

$country = $oldcountry + $newcountry;foreach($country as $countrycode => $countryname){

print $countrycode." - $countryname<br>";}

?>

Page 17: PHP Tutorial (array)

17

Using Array ก�รีรีวมู array เข้��ด�วยก�นำ (array merging)

ก�รีใชุ�ฟั)งก*ชุ� �นำ array_merge() ใชุ�รีวมู array 2 ชุ�ดเข้��ด�วยก�นำ ห�กคื��ในำ array ที่�2งสองที่ �นำ��มู�รีวมูก�นำมู คี�ย์�“ ”ซ้ำ�2�ก�นำ element นำ�2นำจำะถูกแที่นำที่ �ด�วยคื��ข้อง

element ที่ �มู คื ย*ที่ �เหมู�อนำก�นำ

Page 18: PHP Tutorial (array)

18

Using Array ก�รีรีวมู array เข้��ด�วยก�นำ (array merging)

ใชุ�ฟั)งก*ชุ�นำ array_merge() Demo

<? #armerge.php$oldcountry = array("CH" => "Chaina",

"TH" => "Thailand", "IN" => "India",

"JP" => "Japan");$newcountry["CH"] = "Chiangmai";$newcountry["TH"] = "Thibet";$newcountry["HK"] = "Hong Kong";

$country = array_merge($oldcountry , $newcountry);foreach($country as $countrycode => $countryname){

print $countrycode." - $countryname<br>";}

?>

Page 19: PHP Tutorial (array)

19

Using Array ก�รีรีวมู array เข้��ด�วยก�นำ (array merging)

ก�รีใชุ�ฟั)งก*ชุ� �นำ array_merge_recursive() ใชุ�รีวมู array 2 ชุ�ดเข้��ด�วยก�นำ ห�กคื��ในำ array ที่�2งสองที่ �นำ��

มู�รีวมูก�นำมู คื ย*” ”ซ้ำ�2� ก�นำ คื��ที่ �ซ้ำ�2�ก�นำจำะถูกนำ��มู�รีวมูก�นำเป,นำ array ที่ �ซ้ำ�อนำอย�ในำ

array หล�ก มู คื ย* เป,นำเลข้ต่�2งแต่� 0 ไปต่�มูล��ด�บ

Page 20: PHP Tutorial (array)

20

Using Array ก�รีรีวมู array เข้��ด�วยก�นำ (array merging)

ใชุ�ฟั)งก*ชุ�นำ array_merge_recursive() Demo

<? #armrec.php$oldcountry = array("CH" => "Chaina",

"TH" => "Thailand", "IN" => "India",

"JP" => "Japan");$newcountry["CH"] = "Chiangmai";$newcountry["TH"] = "Thibet";$newcountry["HK"] = "Hong Kong";

$country = array_merge_recursive($oldcountry , $newcountry);foreach($country as $countrycode => $countryname){ if(gettype($countryname) == "array"){ print "[$countrycode]:<br>"; foreach($countryname as $key => $cname){ print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[$key] -

$cname<br>"; } }else{ print "[$countrycode] : $countryname<br>";

}}

?>

Page 21: PHP Tutorial (array)

21

Using Array ก�รีห� element ที่ �แต่กต่��งก�นำรีะหว��ง array

ใชุ�ฟั)งก*ชุ� �นำ array_diff() ใชุ�ส��หรี�บห� element ข้อง array ที่ �รีะบ�มู�เป,นำ

argument ต่�วแรีก ที่ �มู คื�� คื ย*“ ” ไมู�ซ้ำ�2�ก�บในำ array ที่ �รีะบ�เป,นำ argument ถู�ดๆ

ไป

Page 22: PHP Tutorial (array)

22

Using Array ก�รีห� element ที่ �แต่กต่��งก�นำรีะหว��ง array

ใชุ�ฟั)งก*ชุ�นำ array_diff() Demo

<? #ardiff.php$oldcountry = array("CH" => "Chaina",

"TH" => "Thailand", "IN" => "India", "JP" => "Japan");

$newcountry["CH"] = "Chaina";$newcountry["TH"] = "Thailand";$newcountry["HK"] = "Hong Kong";

$country = array_diff($oldcountry , $newcountry);foreach($country as $countrycode => $countryname){

print "[$countrycode] : $countryname<br>";}

?>

Page 23: PHP Tutorial (array)

23

Using Array ก�รีห� element ที่ �เหมู�อนำก�นำรีะหว��ง array

ใชุ�ฟั)งก*ชุ� �นำ array_intersect() ใชุ�ส��หรี�บห�คื��สมู�ชุ$กข้อง array ที่ �มู คื�� คี�ย์“ ” ซ้ำ�2�ก�นำ

รีะหว��ง array ต่�2งแต่�สองต่�วข้&2นำไป โดยผลล�พธ์*ที่ �ได�ย�งคืงคื ย*เด$มูเอ�ไว�

Page 24: PHP Tutorial (array)

24

Using Array ก�รีห� element ที่ �แต่กต่��งก�นำรีะหว��ง array

ใชุ�ฟั)งก*ชุ�นำ array_intersect() Demo

<? #arintersect.php$oldcountry = array("CH" => "Chaina",

"TH" => "Thailand", "IN" => "India", "JP" => "Japan");

$newcountry["CH"] = "Chaina";$newcountry["TH"] = "Thailand";$newcountry["HK"] = "Hong Kong";

$country = array_intersect($oldcountry , $newcountry);foreach($country as $countrycode => $countryname){

print "[$countrycode] : $countryname<br>";}

?>

Page 25: PHP Tutorial (array)

25

Using Array ก�รีถูอนำคื�� (pop) และก�รีเพ$�มูคื�� (push) ในำ

array ใชุ�ฟั)งก*ชุ� �นำ array_pop(), array_push()

array_pop() ใชุ�ส��หรี�บห�คื�� element ส�ดที่��ยข้อง array แล�วลบ

element ส�ดที่��ยในำ array นำ�2นำที่$2ง โดยผลล�พธ์*ที่ �ได�ย�งคื ย*เด$มูเอ�ไว�

รีปแบบmixed array_pop(array array)

erray_push() ใชุ�ส��หรี�บเพ$�มูคื�� element ใหมู�เข้��ไปในำ array

รีปแบบint array_push(array array, mixed var[,

mixed.])

Page 26: PHP Tutorial (array)

26

Using Array ก�รีถูอนำคื�� (pop) และก�รีเพ$�มูคื�� (push) ในำ array

ใชุ�ฟั)งก*ชุ�นำ array_pop(), array_push()

Demo

<? #arpop.php

$country = array("CH" => "Chaina",

"TH" => "Thailand",

"IN" => "India",

"JP" => "Japan");

$last = array_pop($country);

print_r($country);

print "<br>POP Item = $last";

?>

Page 27: PHP Tutorial (array)

27

Using Array ก�รีถูอนำคื�� (pop) และก�รีเพ$�มูคื�� (push) ในำ array

ใชุ�ฟั)งก*ชุ�นำ array_pop(), array_push()Demo

<? #arpush.php$country = array("CH" => "Chaina",

"TH" => "Thailand", "IN" => "India", "JP" => "Japan");

$newpush = "USA";$new = array_push($country, $newpush);print_r($country);print "<br>New PUSH Item = $new";

?>

Page 28: PHP Tutorial (array)

28

Using Array ก�รีย��ย-ต่�ด-จำ�ดเรี ยง-เล��อนำ-แที่รีก-แที่นำที่ � element ในำ

array ใชุ�ฟั)งก*ชุ��นำ array_splice()

ในำก�รีใชุ�ง�นำ array เรี�อ�จำมู คืว�มูจำ��เป,นำต่�องลบคื��บ�งคื��ภ�ยในำ

array ออกโดยต่�องก�รีให�มู ก�รีจำ�ดล��ด�บใหมู� หรี�อต่�องก�รีแที่รีก

คื��เข้��ไปในำ array ที่ �มู อย�เด$มูรีปแบบ

array array_splice(array input, int offset [,int length

[,array replacement])

Input คื�ออ�เรีย*ที่ �จำะถูกด��เนำ$นำก�รีOffset คื�อต่�วเลข้รีะบ�ต่��แหนำ�ง element เรี$�มูต่�นำข้องก�รีด��เนำ$นำก�รี

Length คื�อ จำ��นำวนำ element ที่ �จำะถูกด��เนำ$นำก�รี นำ�บต่�2งแต่�ต่��แหนำ�งที่ � offset

Replacement คื�ออ�เรีย*ที่ �จำะนำ��มู�แที่รีกเข้��ในำอ�เรีย*

Page 29: PHP Tutorial (array)

29

Using Array ก�รีย��ย-ต่�ด-จำ�ดเรี ยง-เล��อนำ-แที่รีก-แที่นำที่ � element ในำ array

ใชุ�ฟั)งก*ชุ�นำ array_splice()ก�รีต่�ดบ�ง element ออกและจำ�ดล��ด�บคื ย*ใหมู�ด�วยฟั)งก*ชุ�นำ array_splice()

Demo<? #arsplice.php

$country = array("CH" => "Chaina", "TH" => "Thailand", "IN" => "India", "JP" => "Japan");

print "Before splice...<br>";foreach($country as $countrycode => $countryname){

print("$countrycode => $countryname<br>");}array_splice($country, 1, 2);print "<br>After splice...<br>";foreach($country as $countrycode => $countryname){

print("[$countrycode] => $countryname<br>");}

?>

Page 30: PHP Tutorial (array)

30

Using Array ก�รีย��ย-ต่�ด-จำ�ดเรี ยง-เล��อนำ-แที่รีก-แที่นำที่ � element ในำ array

ใชุ�ฟั)งก*ชุ�นำ array_splice()ก�รีแที่นำที่ �คื��ในำ array ด�วยฟั)งก*ชุ�นำ array_splice()

Demo<? #arsplice02.php

$country = array("CH" => "Chaina", "TH" => "Thailand", "IN" => "India", "JP" => "Japan");

$other = array("Hong Kong", "Korea");print "Before splice...<br>";foreach($country as $countrycode => $countryname){

print("$countrycode => $countryname<br>");}$remove = array_splice($country, 1, 2, $other);print "<br>After splice...<br>";foreach($country as $countrycode => $countryname){

print("[$countrycode] => $countryname<br>");}print "<br>Remove elements...<br>";foreach($remove as $countrycode => $countryname){

print("[$countrycode] => $countryname<br>");}

?>

Page 31: PHP Tutorial (array)

31

Using Array ก�รีนำ�บจำ��นำวนำ element ในำ array

ใชุ�ฟั)งก*ชุ��นำ count()ใชุ�นำ�บจำ��นำวนำ element ในำ array

รีปแบบ int count(mixed var [, int mode])

var คื�อต่�วแปรีหรี�อคื��อ�เรีย*ที่ �ต่�องก�รีนำ�บจำ��นำวนำ element

Demo<? #count.php

$country = array("CH" => "Chaina", "TH" => "Thailand",

"IN" => "India", "JP" => "Japan");

print count($country);?>

Page 32: PHP Tutorial (array)

32

Using Array ก�รีต่�วชุ 2กล�บไปที่ � element แรีกข้อง array

ใชุ�ฟั)งก*ชุ��นำ reset()

ใชุ�เล��อนำต่�วชุ 2กล�บไปที่ � element แรีกข้อง array

Demo

<? #reset.php

$country = array("CH" => "Chaina",

"TH" => "Thailand",

"IN" => "India",

"JP" => "Japan");

print count($country)."<br>";

print reset($country);

?>

Page 33: PHP Tutorial (array)

33

Using Array ก�รีอ��นำคื��ในำ array

ใชุ�ฟั)งก*ชุ��นำ each()จำะให�คื��เป,นำ คื ย* และ element ข้อง array ที่ �ต่�วชุ 2ที่��ก�รีชุ 2

อย�ในำ ข้ณะนำ�2นำพรี�อมูที่�2งจำะเล��อนำต่�วชุ 2ไป element ถู�ดไป

Demo<? #each.php

$country = array("CH" => "Chaina","TH" => "Thailand","IN" => "India","JP" => "Japan");

while($val = each($country)){print $val[key]." => ".$val[value]."<br>";

}?>

Page 34: PHP Tutorial (array)

34

Using Array ก�รีนำ��คื��ในำ array ใส�ลงในำต่�วแปรี

ใชุ�ฟั)งก*ชุ� �นำ list()เป,นำว$ธ์ ก�รีในำก�รีนำ��คื��ข้องแต่�ละ element ข้อง

array มู�แยก ลงในำต่�วแปรีแต่�ละต่�ว เมู��อที่รี�บจำ��นำวนำ element

แนำ�นำอนำ

Demo<? #list.php

list($day, $month, $year) = explode("-",date("d-m-Y"));

print " \$day = ".$day."<br>";print " \$month = ".$month."<br>";print " \$year = ".$year."<br>";

?>

Page 35: PHP Tutorial (array)

35

Using Array ก�รีที่��ล�ยต่�วแปรี array

ใชุ�ฟั)งก*ชุ� �นำ unset()เป,นำว$ธ์ ก�รียกเล$กต่�วแปรี array หรี�อเป,นำก�รีคื�นำหนำ�วยคืว�มูจำ��ให�ก�บ

คือมูพ$วเต่อรี*

Demo <? #unset.php

$country = array("CH" => "Chaina", "TH" => "Thailand", "IN" => "India", "JP" => "Japan");

unset($country);//print_r($country);while(list($countrycode, $countryname) = each($country)){

print $countrycode." => ".$countryname."<br>";

}?>

Page 36: PHP Tutorial (array)

36

Using Array array 2 มู$ต่$

ในำ php ไมู�มู ข้�อจำ��ก�ดเก �ยวก�บข้นำ�ดข้องมู$ต่$ข้อง array ข้&2นำอย�ก�บหนำ�วยคืว�มูจำ��

Demo<?#dim.php

$number = array(2 => array(4,6,8), 3 => array(6,9), 4 => array(8));print "<table border=1>";print "<tr>";foreach($number as $parent => $child){

print "<td align='center' colspan='".count($number[$parent])."'>".$parent;

}print "</td></tr><tr>";foreach($number as $parent => $child){

foreach($child as $key => $value){print "<td align='center'>".$value;

}}print "</td></tr></table>";

?>

Page 37: PHP Tutorial (array)

37

ฟั)งก*ชุ��นำเก �ยวก�บเวล� date()

เป,นำฟั)งก*ชุ�นำที่ �ใชุ�รี�บว�นำที่ �และเวล� ณ ป)จำจำ�บ�นำข้องเคืรี��องคือมูพ$วเต่อรี* ส�มู�รีถูก��หนำดรีปแบบข้องว�นำที่ �และเวล�ได�

ร์�ปแบบString date (string format [, int timestamp])

format คื�อรีปแบบว�นำที่ �และเวล�ซ้ำ&�งจำะแที่นำด�วยต่�วอ�กษรีภ�ษ�อ�งกฤษ 1 ต่�ว

d แที่นำว�นำที่ �เลข้ 2 หล�ก 01-31 D แที่นำว�นำในำส�ปด�ห* Sun-Sat

m แที่นำเด�อนำเลข้ 2 หล�ก 01-12 M แที่นำชุ��อย�อข้องเด�อนำ Jan-Dec

n แที่นำเด�อนำไมู�มู 0 นำ��หนำ�� 1-12

y แที่นำป9 คื.ศ. เลข้ 2 หล�ก 98,99,05 Y แที่นำป9 คื.ศ. เลข้ 4 หล�ก 1998

1999, 2005

timestamp เป,นำต่�วเลข้ integer ล��ด�บเวล�ที่�กหนำ&�งว$นำ�ที่

Page 38: PHP Tutorial (array)

38

ฟั)งก*ชุ��นำเก �ยวก�บเวล�Demo

<?#date.php

print date("d/m/Y")."<br>";

print date("d/m/Y H:i:s")."<br>";

print date("D d M Y h:i:s a")."<br>";

?>

Page 39: PHP Tutorial (array)

39

ฟั)งก*ชุ��นำเก �ยวก�บเวล� getdate()

เป,นำฟั)งก*ชุ�นำที่ �ใชุ�รี�บว�นำที่ �และเวล� ณ ป)จำจำ�บ�นำข้องเคืรี��องคือมูพ$วเต่อรี* โดยฟั)งก*ชุ�นำจำะให�ผลล�พธ์*ที่ �เป,นำ array ข้องว�นำ เด�อนำ ป9

และเวล�ในำรีปแบบต่��งๆ รีปแบบ

array getdate([int timestamp]) timestamp เป,นำต่�วเลข้ integer ล��ด�บเวล�ที่�กหนำ&�งนำ�ที่

โดย getdate จำะคื�นำคื��มู�เป,นำ array จำ��นำวนำ 10 elements ได�แก�

second – ว$นำ�ที่ minutes – นำ�ที่ hours – ชุ��วโมูง

mday – ว�นำที่ � wday – ว�นำในำส�ปด�ห*(0-6) mon – เลข้เด�อนำ

year – ป9 คื.ศ. yday – เลข้ว�นำที่ �ข้องป9 weekday – ชุ��อว�นำ month - ชุ��อเด�อนำ

Page 40: PHP Tutorial (array)

40

ฟั)งก*ชุ��นำเก �ยวก�บเวล�

Demo

<?#getdate.php

$nowdate = getdate();

$today = $nowdate['mday']."/";

$today.=$nowdate['mon']."/";

$today.=$nowdate['year'];

print $today."<br>";

?>

Page 41: PHP Tutorial (array)

41

ฟั)งก*ชุ��นำเก �ยวก�บเวล� getdate()

//…

$nowdate = getdate();’

print “ณ ว�นำ”;

switch($nowdate[‘wday’]){

case 0: $day = “อ�ที่$ต่ย*”;break;

case 1: $day = “จำ�นำที่รี*”;break;

case 2: $day = “อ�งคื�รี”;break;

//…

}

Page 42: PHP Tutorial (array)

42

ฟั)งก*ชุ��นำเก �ยวก�บเวล� getdate()

switch($nowdate[‘mon’]){

case 1: $ month = “มูกรี�คืมู”;break;

case 2: $ month = “ก�มูภ�พ�นำธ์*”;break;

case 3: $ month = “มู นำ�คืมู”;break;

//…

}

$year=$nowdate[‘year’]+543;

Print $day.” ที่ � “.$nowdate[‘mday.” “.$month];

Print “พ.ศ. “.$year.”<br>”;

Page 43: PHP Tutorial (array)

43

ฟั)งก*ชุ��นำเก �ยวก�บเวล� time(), mktime()

เป,นำฟั)งก*ชุ��นำที่ �ใชุ�รี�บเวล� ณ ป)จำจำ�บ�นำข้องเคืรี��องคือมูพ$วเต่อรี*Demo

<?#time.phpprint "time ".time()." = ".date("H:i:s", time())."<br>";print "mktime = ".mktime()." = " .date("H:i:s",

mktime())."<br>";?ที่�2งสองฟั)งก*ชุ��นำต่��งก�นำต่รีงที่ � mktime() ส�มู�รีถูรี�บ argument ต่�วเลข้ว�นำที่ � และเวล�เข้��ไปเพ��อสรี��ง timestamp ต่�มูว�นำที่ �และเวล�นำ�2นำได�รีปแบบInt mktime(int hour, int minute, int second, int mohth, int day, int year[, int is_dst])

Page 44: PHP Tutorial (array)

44

ฟั)งก*ชุ��นำเก �ยวก�บเวล� time(), mktime()

Demo

<?#mktime.php

$mktime = mktime(13,20,10,3,28,2005);

print "mktime = ".$mktime." = ".date("H:i:s", $mktime)."<br>";

?>

Page 45: PHP Tutorial (array)

45

ฟั)งก*ชุ��นำเก �ยวก�บเวล� strtotime()

เป,นำฟั)งก*ชุ� �นำที่ �ใชุ�รี�บคื��ว�นำที่ �และเวล�ด�วยข้�อคืว�มู หล�กหล�ยรีปแบบ

Demo<?#strtotime.php

print "date-time ".date(" d-m-Y H:i:s", strtotime("28 Mar 2005"))."<br>";

print "date-time" .date(" d-m-Y H:i:s", strtotime("-2 hours"))."<br>";

print "date-time" .date(" d-m-Y H:i:s", strtotime("+2 days"))."<br>";

print "date-time" .date(" d-m-Y H:i:s", strtotime("last year"))."<br>";

?>

Page 46: PHP Tutorial (array)

46

ฟั)งก*ชุ��นำเก �ยวก�บเวล� gmdate()

เป,นำฟั)งก*ชุ� �นำที่ �ใชุ�ในำก�รีห�ว�นำที่ �และเวล�มู�ต่รีฐ�นำกรี นำ$ชุ ใชุ�ง�นำเหมู�อนำ

ฟั)งก*ชุ� �นำ date()

Checkdate()เป,นำฟั)งก*ชุ� �นำที่ �ใชุ�ที่��หนำ��ที่ �ต่รีวจำสอบคืว�มูถูกต่�องข้อง

ข้�อมูลปรีะเภที่ว�นำที่ �โดยมู เง��อนำไข้ก�รีต่รีวจำสอบคื�อ

ป9 คื.ศ. ต่�2งแต่� 1 ถู&ง 32766 เด�อนำ ต่�2งแต่� 1 ถู&ง 12

ว�นำที่ � ที่ �ถูกต่�องข้องเด�อนำนำ�2นำๆ รีปแบบ

bool checkdate(int month, int date, int year)

Page 47: PHP Tutorial (array)

47

ฟั)งก*ชุ��นำเก �ยวก�บเวล� checkdate()

Demo

<?#checkdate.php

$gmdate = gmdate("d-m-Y");

print $gmdate." เวล� GMT ".gmdate("H:i:s")."<br>";

list($mday, $mmonth, $myear)=explode("-",$gmdate);

if(checkdate($mmonth, $mday, $myear)){

print "The date $gmdate is valid.";

}else{

print "$gmdate is invalid date";

}

?>

Page 48: PHP Tutorial (array)

48

Using Regular Expression ใชุ�ต่รีวจำสอบรีปแบบข้องข้�อมูลที่ �ผ�ใชุ�ป<อนำเข้��เข้��

รีะบบ เชุ�นำ หมู�ยเลข้โที่รีศ�พที่*, e-mail address และ URL ข้องเว�บเพจำ เป,นำต่�นำ

ส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ (not complete)

ส�ญล�กษณ* ^ใชุ�ต่รีวจำสอบต่�วอ�กษรีหรี�อข้�อคืว�มูที่ �สอดคืล�องก�บรีปแบบที่ �อย�ข้��งหนำ��เคืรี��องหมู�ย ^ เชุ�นำ (ต่รีวจำสอบรีปแบบข้��ง

หล�งเคืรี��องหมู�ย)

“http^”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง

Page 49: PHP Tutorial (array)

49

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ (complete)

ส�ญล�กษณ* $ใชุ�ต่รีวจำสอบต่�วอ�กษรีหรี�อข้�อคืว�มูส�ดที่��ยที่ �สอดคืล�องก�บรีปแบบที่ �อย�หนำ��เคืรี��องหมู�ย $ เชุ�นำ (ต่รีวจำสอบรีปแบบ

ข้��งหนำ��สอดคืล�องก�บอ�กษรีที่ �ก��หนำด)

“com$”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ“http://www.google.com” จำะให�ผลล�พธ์*เป,นำจำรี$ง

Regexp02.php

Page 50: PHP Tutorial (array)

50

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* .ใชุ�ต่รีวจำสอบต่�วอ�กษรีใดๆ ก�ได�จำ��นำวนำหนำ&�งต่�วอ�กษรี เชุ�นำ

“.”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง regexp03.php //not worked

“t”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง regexp04.php

Page 51: PHP Tutorial (array)

51

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ (complete)

ส�ญล�กษณ* |เที่��ก�บคื��ว�� หรี�อ ใชุ�ต่รีวจำสอบต่�วอ�กษรีหรี�อข้�อคืว�มูที่ �อย�

หนำ��ต่�วอ�กษรีหรี�อส�ญล�กษณ*นำ 2 เชุ�นำ “cat|dog”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง

regexp05.php

Page 52: PHP Tutorial (array)

52

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ (not

complete)ส�ญล�กษณ* ?

ใชุ�เปรี ยบเที่ ยบส�วนำใดส�วนำหนำ&�งข้องข้�อคืว�มูห�กสอดคืล�องก�บต่�วอ�กษรีหรี�อรีปแบบที่ �อย�หนำ��เคืรี��องหมู�ยคื��ถู�มูนำ 2จำ��นำวนำศนำย*หรี�อหนำ&�งคืรี�2งจำะให�ผลล�พธ์*เป,นำจำรี$ง

เชุ�นำ “t?”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ

“ot?.c”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ

Page 53: PHP Tutorial (array)

53

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* *ใชุ�เปรี ยบเที่ ยบส�วนำใดส�วนำหนำ&�งข้องข้�อคืว�มูห�ก

สอดคืล�องก�บต่�วอ�กษรีหรี�อรีปแบบที่ �อย�หนำ��เคืรี��องดอกจำ�นำนำ 2จำ��นำวนำศนำย*หรี�อมู�กกว��หนำ&�งคืรี�2งจำะให�ผลล�พธ์*

เป,นำจำรี$ง เชุ�นำ “to*t”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ “http://www.tatoothai.com” จำะให�ผลล�พธ์*เป,นำ

จำรี$ง

Page 54: PHP Tutorial (array)

54

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* +ใชุ�เปรี ยบเที่ ยบส�วนำใดส�วนำหนำ&�งข้องข้�อคืว�มูที่ �สอดคืล�องก�บต่�วอ�กษรีหรี�อรีปแบบที่ �อย�หนำ��เคืรี��องหมู�ยนำ 2จำ��นำวนำ

หนำ&�งคืรี�2งหรี�อมู�กกว�� เชุ�นำ “to+t”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ “http://www.tatoothai.com” จำะให�ผลล�พธ์*เป,นำ

จำรี$ง

Page 55: PHP Tutorial (array)

55

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* \ใชุ�เพ��อนำ��หนำ��ต่�วอ�กษรีพ$เศษได�แก� ^ $ . | ? + \ [ ] ( ) { } ซ้ำ&�งในำกล��มูอ�กข้รีะพ$เศษใชุ�เป,นำส�ญล�กษณ*ที่ �ส��อคืว�มูหมู�ย

อ��นำ เชุ�นำ “\.com”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ “http://www.tatoothai.com” จำะให�ผลล�พธ์*เป,นำ

จำรี$ง

Page 56: PHP Tutorial (array)

56

Using Regular Expression ส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* [ ]ใชุ�แสดงกล��มูต่�วอ�กษรีหรี�อกล��มูคื��ในำก�รีเปรี ยบเที่ ยบ เชุ�นำ

[a-z] หมู�ยคืว�มูว�� ข้�อคืว�มูที่ �นำ��มู�เปรี ยบเที่ ยบมู ต่�วอ�กษรีต่�วใดต่�วหนำ&�งอย�รีะหว��ง a-z ก�จำะให�ผลล�พธ์*เป,นำจำรี$ง[A-Z0-9] หมู�ยคืว�มูว��ข้�อคืว�มูที่ �นำ��มู�เปรี ยบเที่ ยบมู ต่�วอ�กษรีต่�วใดต่�วหนำ&�งเป,นำ ต่�วอ�กษรี A-Z หรี�อ ต่�วเลข้ 0-9 ก�

จำะให�ผลล�พธ์*เป,นำจำรี$งต่�วอย��ง เชุ�นำ

“[s-z]ot“

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง

Page 57: PHP Tutorial (array)

57

Using Regular Expression ส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* [ ]ห�กมู ส�ญล�กษณ* ^ อย�ในำเคืรี��องหมู�ย [ ] นำ 2 จำะให�

คืว�มูหมู�ยต่รีงก�นำข้��มู เชุ�นำ“[^s-z]ot“

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ “http://www.aot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง

Page 58: PHP Tutorial (array)

58

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* ( )ใชุ�ในำก�รีจำ�ดกล��มูต่�วอ�กษรีให�เป,นำกล��มูเด ยวก�นำซ้ำ&�งห�กมู ส�ญล�กษณ* ^ นำ��หนำ��หรี�อมู ส�ญล�กษณ* $ | ? * + ต่�มู

หล�ก) ก�จำะปรีะมูวลผลเสมู�อนำว��กล��มูต่�วอ�กษรีในำวงเล�บนำ 2เป,นำต่�วอ�กษรีเด ยวก�นำ เชุ�นำ

“(tot\.)”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำเที่�จำ “http://www.tatoothai.com” จำะให�ผลล�พธ์*เป,นำ

เที่�จำ

Page 59: PHP Tutorial (array)

59

Using Regular Expressionส�ญล�กษณ*ที่ �ใชุ�ในำก�รีสรี��งรีปแบบ

ส�ญล�กษณ* { }ใชุ�ก��หนำดจำ��นำวนำต่�วอ�กษรีที่ �อย�ข้��งหนำ�� เมู��อนำ��ข้�อคืว�มูมู�

เปรี ยบเที่ ยบ เชุ�นำ “w{3}”

“http://www.tot.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง“http://www.cat.co.th” จำะให�ผลล�พธ์*เป,นำจำรี$ง

“http://www.tatoothai.com” จำะให�ผลล�พธ์*เป,นำจำรี$ง

Page 60: PHP Tutorial (array)

60

Using Regular Expression ฟั)งก*ชุ� �นำ ereg()

ใชุ�เปรี ยบเที่ ยบข้�อคืว�มูรีปแบบ regular expression ที่ �ก��หนำดไว�ว��สอดคืล�องก�นำหรี�อไมู� ถู��สอดคืล�องก�นำ จำะให�

คื��เป,นำจำรี$งรีปแบบ

Int ereg(string pattern, string string[, array regs])

pattern รีปแบบข้อง regular expression

string ข้�อคืว�มูที่ �ต่�องก�รีนำ��มู�ต่รีวจำสอบregs ต่�วแปรี array ที่ �เก�บส�วนำปรีะกอบข้อง

ข้�อคืว�มูที่ �นำ��มู�ต่รีวจำสอบ เฉพ�ะในำส�วนำที่ �สอดคืล�องก�บรีปแบบ regular expression ที่ �อย�ในำเคืรี��องหมู�ยวงเล�บ

Page 61: PHP Tutorial (array)

61

Using Regular ExpressionDemo

<?#ereg01.php

$text = "http://www.tot.co.th";

$pattern = "^(http)://w{3}\.[a-z][a-z0-9\_\-]*\.(co\.th|com|net)$";

if(ereg($pattern, $text, $regs)){

print("$text is valid.<br>");

foreach($regs as $ar_reg){

print("$ar_reg<br>");

}

}else{

print("Sorry! $text is not valid.<br>");

}

?>

Page 62: PHP Tutorial (array)

62

Using Regular Expression ฟั)งก*ชุ� �นำ eregi()

ใชุ�เปรี ยบเที่ ยบข้�อคืว�มูรีปแบบ regular expression ที่ �ก��หนำดไว�ว��สอดคืล�องก�นำหรี�อไมู� ถู��สอดคืล�องก�นำ จำะให�คื��เป,นำจำรี$ง เหมู�อนำ ereg() แต่�จำะถู�อว��ต่�วอ�กษรีพ$มูพ*เล�ก

พ$มูพ*ใหญ�เหมู�อนำ (case insensitive)

รีปแบบInt eregi(string pattern, string string[, array regs])

Page 63: PHP Tutorial (array)

63

Using Regular ExpressionDemo

<?#ereg02.php

$text = "http://www.TOT.co.th";

$pattern = "^(http)://w{3}\.[a-z][a-z0-9\_\-]*\.(co\.th|com|net)$";

if(eregi($pattern, $text, $regs)){

print("$text is valid.<br>");

foreach($regs as $ar_reg){

print("$ar_reg<br>");

}

}else{

print("Sorry! $text is not valid.<br>");

}

?>

Page 64: PHP Tutorial (array)

64

Using Regular Expression ฟั)งก*ชุ� �นำ ereg_replace()

ใชุ�แที่นำที่ �ข้�อคืว�มูย�อยที่ �สอดคืล�องก�บ regular expression ด�วยข้�อคืว�มูที่ �รีะบ�ให� case sensitive

รีปแบบstring ereg_replace(string pattern, string replacement,

string string)

pattern รีปแบบข้อง regular expression

string ข้�อคืว�มูที่ �ต่�องก�รีนำ��มู�ต่รีวจำสอบreplacement ข้�อคืว�มูใหมู�ที่ �ต่�องก�รีนำ��มู�แที่นำที่ �

Page 65: PHP Tutorial (array)

65

Using Regular ExpressionDemo

<?#ereg03.php

$text = "http://www.tot.co.th";

echo ereg_replace(“co.th", “or.th", $text);

?>

Page 66: PHP Tutorial (array)

66

Using Regular Expression ฟั)งก*ชุ� �นำ eregi_replace()

ใชุ�แที่นำที่ �ข้�อคืว�มูย�อยที่ �สอดคืล�องก�บ regular expression ด�วยข้�อคืว�มูที่ �รีะบ�ให�โดย case insensitive

รีปแบบstring eregi_replace(string pattern, string replacement,

string string)

pattern รีปแบบข้อง regular expression

string ข้�อคืว�มูที่ �ต่�องก�รีนำ��มู�ต่รีวจำสอบreplacement ข้�อคืว�มูใหมู�ที่ �ต่�องก�รีนำ��มู�แที่นำที่ �

Page 67: PHP Tutorial (array)

67

Using Regular ExpressionDemo

<?#ereg04.php

$text = "http://www.tot.CO.TH";

echo eregi_replace(“co.th", “or.th", $text);

?>

Page 68: PHP Tutorial (array)

68

Using Regular Expression ฟั)งก*ชุ� �นำ split()

ใชุ�แบ�งแยกข้�อคืว�มูที่ �ถูกคื��นำด�วยส�ญล�กษณ* หรี�อข้�อคืว�มูย�อยที่ �สอดคืล�องก�บ regular expression และคื�นำคื��เป,นำ array

รีปแบบarray split(string pattern, string string, [,int limit])

pattern รีปแบบข้อง regular expression

string ข้�อคืว�มูที่ �ต่�องก�รีนำ��มู�แบ�งlimit ก��หนำดจำ��นำวนำ element ข้อง array

Page 69: PHP Tutorial (array)

69

Using Regular ExpressionDemo

<?#ereg05.php

$text = "http://www.tot.co.th";

list($http, $url) = split("//", $text);

echo $url;

print $http;

?>