ถามเรื่อง วน loop ตัวแปร อะเรย์ครับ

เริ่มโดย rungroat4, 15 ธันวาคม 2013, 18:57:25

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

rungroat4

ผม มี array ดังนี้ครับ
$id=array("560001", "560002", "560003", "560004" , "560006" , "560007");

ผมต้องการ วนลูป มา เพื่อให้แสดงเป็น

560001 - 560004 , 560006 - 560007

ลองเขียน code แล้วมัน ได้แค่นี้

560001 - 560004

นี่โค้ดที่ผมเขียน แก้ยังไงดีครับ

$id=array("560001", "560002", "560003", "560004" , "560006" , "560007");
for($a=0 ;$a<count($id) ; $a++ )
{
if(!$first_arr)
{
            $first_arr=$id[$a];
}

if($id[$a]==$next_short)
{
$last_arr=$id[$a];
}

$next_short=$id[$a]+1;
         }

echo "$first_arr - $last_arr";
[direct=http://www.siamtime.co.th/]นาฬิกา[/direct]เท่ห์ ๆ ของแท้ ราคาถูก ประกันศูนย์

ossytong

#1
---

rungroat4

[direct=http://www.siamtime.co.th/]นาฬิกา[/direct]เท่ห์ ๆ ของแท้ ราคาถูก ประกันศูนย์

Surakrai

#3
if($a == 0)
{
   echo $id[$a] ;
}   
elseif($a == 3)
{
   echo '-' . $id[$a] . ',' ;
}
elseif($a == 4) {
   echo $id[$a].'-' ;

}elseif($a == count($id)-1){
   echo $id[$a] ;
}
เช็คแบบนี้มันจะ hard code ไปไหม

insidecom

อ้างถึง$id=array("560001", "560002", "560003", "560004" , "560006" , "560007", "560010" , "560011");
   $result="";
   for($a=0 ;$a<count($id) ; $a++ )
       {      
                  if($first_arr=="")
                  {
                     $first_arr=$id[$a];
                  }
                  
                  if($id[$a]==$next_short)
                  {
                     $last_arr=$id[$a];
                  }elseif($id[$a]!=$next_short && $a>0 ){
                     $result .="$first_arr - $last_arr ,";
                     $first_arr=$id[$a];
                  }
                  
                  if(($a+1)==count($id)) { $result .="$first_arr - $last_arr "; }
                  $next_short=$id[$a]+1;
                           
         }
       echo $result;
แก้ไขให้แล้วครับ ลองดูนะครับ ผมว่านะจะประมาณนี้
ผมลองเพิ่ม  "560010" , "560011" ก้อได้นะครับ

rungroat4

ขอบคุณทุกคนที่ช่วยตอบ และ
ขอบคุณ คุณ insidecom  มากครับ

เกือบได้แล้วครับ

ผมลอง เพิ่ม array ที่เป็น 2 ตัว

$id=array("560001", "560002", "560003", "560004" , "560006" , "560007", "560010" , "560011", "560013" , "560016");
   $result="";
   for($a=0 ;$a<count($id) ; $a++ )
       {     
                  if($first_arr=="")
                  {
                     $first_arr=$id[$a];
                  }
                 
                  if($id[$a]==$next_short)
                  {
                     $last_arr=$id[$a];
                  }elseif($id[$a]!=$next_short && $a>0 ){
                     $result .="$first_arr - $last_arr ,";
                     $first_arr=$id[$a];
                  }
                 
                  if(($a+1)==count($id)) { $result .="$first_arr - $last_arr "; }
                  $next_short=$id[$a]+1;
                           
         }
       echo $result;
       
       ?>


เพิ่ม "560013" , "560016" อีก มันออกมาเป็น


560001 - 560004 ,560006 - 560007 ,560010 - 560011 ,560013 - 560011 ,560016 - 560011




ที่ถูกมันควรจะเป็น

560001 - 560004 ,560006 - 560007 ,560010 - 560011 ,560013 ,560016
ครับ แก้ยังไงดีครับ


[direct=http://www.siamtime.co.th/]นาฬิกา[/direct]เท่ห์ ๆ ของแท้ ราคาถูก ประกันศูนย์

insidecom

#6
อ้างถึง$id=array("560001", "560002", "560003", "560004" , "560006" , "560007", "560008" , "560010" , "560012");
  $result="";
  for($a=0 ;$a<count($id) ; $a++ )
      {      
                 if($first_arr=="")
                 {
                    $first_arr=$id[$a];
                 }
                 
                 if($id[$a]==$next_short)
                 {
                    $last_arr=$id[$a];
                 }elseif($id[$a]!=$next_short && $a>0 ){
                 
                      if($last_arr!="") $result .="$first_arr - $last_arr ,";
                else $result .="$first_arr ,";
               
                $last_arr = "";
                    $first_arr=$id[$a];
                 }
                 
                 if(($a+1)==count($id)) {
                 if($last_arr!="") $result .="$first_arr - $last_arr ";
               else $result .="$first_arr ";
               }
                 $next_short=$id[$a]+1;
                         
        }
      echo $result;
ลองดูอีกทีนะครับ น่าจะได้   ออกแนว hard code หน่อยนะครับ ลองปรับใช้ดูครับ :wanwan003:

rungroat4

ได้แล้วครับ +1 ให้แล้วนะครับ เมื่อวันก่อน
ขอบคุณ คุณ insidecom  มากครับ

งมมา เป็นเดือนละครับ

ในนี้ มีคนเก่งเยอะดีครับ

:wanwan031:
[direct=http://www.siamtime.co.th/]นาฬิกา[/direct]เท่ห์ ๆ ของแท้ ราคาถูก ประกันศูนย์


goldxp


abstract class StartEnd{
    protected $m = array();
    public function push($data){
        if (count($this->m)==0){
           $this->m[1] = $this->m[0] = $data;
        }else{
           $this->m[1] = $data;
        }
    }
    public function __toString(){
        if (count($this->m)==0){
            return '';
        }elseif ($this->m[0] == $this->m[1]){
            return (string)$this->m[0];
        }else{
            return $this->m[0].'-'.$this->m[1];
        }
    }
    public static function getFormat(array $a){
        $groups = array();
        $prev = -1;
        static::sort($a);
        foreach($a as $i){
              if ($prev !== static::getPrev($i)){
                     $se = new static();
                     $groups[] = $se;
              }
              $se->push($prev = $i);
        }
        return implode(',',$groups);
    }
    abstract public static function sort(&$a);
    abstract public static function getPrev($i);
}
class StartEndNumber extends StartEnd{
    public static function sort(&$a){
        sort($a,SORT_NUMERIC);
    }
    public static function getPrev($i){
        return $i-1;
    }
}
class StartEndDay extends StartEnd{
    protected static $enum = array('MON','TUE','WED','THU','FRI','SAT','SUN');
    public static function daySort($val1,$val2){
        return array_search($val1,static::$enum)-array_search($val2,static::$enum);
    }
    public static function sort(&$a){
        usort($a,'StartEndDay::daySort');
    }
    public static function getPrev($i){
        $position = array_search($i,self::$enum);
        if ($position === false){
                return false;
        }else{
                return self::$enum[$position-1];
        }
    }
}


$a = array(1,4,3,2,5,9,10,12);
echo StartEndNumber::getFormat($a),' '; //1-5,9-10,12

$a = array('TUE','MON','WED','FRI','SAT');
echo StartEndDay::getFormat($a); //MON-WED,FRI-SAT

http://web-programming-bookmark.blogspot.com/2013/12/start-end-format-display-number.html

โค้ดอีกแบบหนึ่งครับ  :wanwan017:
[direct=http://web-programming-bookmark.blogspot.com/p/blog-page.html]รับแก้ไขเว็บ[/direct]