menu

Hot Offer

Hot Offer
Hot Offer
แสดงบทความที่มีป้ายกำกับ ขี้เกียจจำ แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ ขี้เกียจจำ แสดงบทความทั้งหมด

function Sort() ใน PHP (ขี้เกียจจำ)

function Sort() ใน PHP

สามารถ Sort ได้ทั้ง ตัวเลขและตัว Alphabet ดังตัวอย่างข้างล่างนี้
//----------------------ตัวเลข
<?php

   $input = array(14,23,45,46,38,16,5,29,41,43,19,35,20);

    if(sort($input)){
     $input1 = array(14,23,45,46,38,16,5,29,41,43,19,35,20);
    $i = 0;
     foreach($input as $key=>$value){
     
     echo "beForeSort = $input1[$i] afterSort[$key] = ".$value."< br >";
     $i++;
     }
       
   }

?>

Result :
beForeSort = 14 afterSort[0] = 5
beForeSort = 23 afterSort[1] = 14
beForeSort = 45 afterSort[2] = 16
beForeSort = 46 afterSort[3] = 19
beForeSort = 38 afterSort[4] = 20
beForeSort = 16 afterSort[5] = 23
beForeSort = 5 afterSort[6] = 29
beForeSort = 29 afterSort[7] = 35
beForeSort = 41 afterSort[8] = 38
beForeSort = 43 afterSort[9] = 41
beForeSort = 19 afterSort[10] = 43
beForeSort = 35 afterSort[11] = 45
beForeSort = 20 afterSort[12] = 46


//----------------------------Alphabet
<?php
$input = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Apple","Google","Banana");

    if(sort($input)){
     $input1 = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Apple","Google","Banana");
    $i = 0;
     foreach($input as $key=>$value){
     
     echo "beForeSort = $input1[$i] afterSort[$key] = ".$value."< br >";
     $i++;
     }
       
   }

?>

Result :

beForeSort = Sunday afterSort[0] = Apple
beForeSort = Monday afterSort[1] = Banana
beForeSort = Tuesday afterSort[2] = Friday
beForeSort = Wednesday afterSort[3] = Google
beForeSort = Thursday afterSort[4] = Monday
beForeSort = Friday afterSort[5] = Saturday
beForeSort = Saturday afterSort[6] = Sunday
beForeSort = Apple afterSort[7] = Thursday
beForeSort = Google afterSort[8] = Tuesday
beForeSort = Banana afterSort[9] = Wednesday