首页 > 代码库 > php瀑布流,把一个数组分4个数组,按照时间排序

php瀑布流,把一个数组分4个数组,按照时间排序

简单介绍:把一个数组分成4个数组,取其中1的倍数

<?php$arr = array(    0 => 0,    1 => 1,    2 => 2,    3 => 3,                4 => 4,    5 => 5,    6 => 6,    7 => 7,                8 => 8,    9 => 9,    10 => 10,    11 => 11,                12 => 12,            );

foreach($arr as $k=>$val){
  for($i=0;$i<4;$i++){
    $a = $i*4+1;           // 其中的1是倍数,可以是0、1、2、3不能超过4;如果是7,则$i<7;
    if($k==$a){
      $item[] = $val;
    }
  }
}

?>

输出结果
Array ( [0] => 1 [1] => 5 [2] => 9 );

 

简单介绍:把一个数组分成4个数组,取其中3的倍数

<?php$arr = array(    0 => 0,    1 => 1,    2 => 2,    3 => 3,                4 => 4,    5 => 5,    6 => 6,    7 => 7,                8 => 8,    9 => 9,    10 => 10,    11 => 11,                12 => 12,            );foreach($arr as $k=>$val){  for($i=0;$i<4;$i++){    $a = $i*4+1;           // 其中的1是倍数,可以是0、1、2、3不能超过4;如果是7,则$i<7;    if($k==$a){      $item[] = $val;        }      }}?>输出结果Array ( [0] => 3 [1] => 7 [2] => 11 )

 

 

与数据库结合

class IndexAction extends BaseAction{    public function index()    {                $m = M(article);        $res = $m->select();        $f = $this->d($res,2);        print_r($f);            }        public function d($arr,$v){        foreach($arr as $k=>$val){            for($i=0;$i<4;$i++){                $a = $i*4+$v;                if($k==$a){                    $item[] = $val;                    }                }        }        return $item;            }     }

输出结果
Array ( [0] => Array ( [id] => 3 [catid] => 1 [userid] => 1 [username] => yourphp [title] => 欧美风2014夏新款韩国名媛精致复合提花拼纱荷叶袖连衣裙 [title_style] => [keywords] => [copyfrom] => [fromlink] => [description] => 欧美风2014夏新款韩国名媛精致复合提花拼纱荷叶袖连衣裙 [content] => 欧美风2014夏新款韩国名媛精致复合提花拼纱荷叶袖连衣裙 [template] => 0 [thumb] => /b/Uploads/201407/53d220e49655a.jpg [posid] => 0 [status] => 1 [recommend] => 1 [readgroup] => [readpoint] => 0 [listorder] => 0 [url] => /b/index.php?m=Article&a=show&id=3 [hits] => 0 [createtime] => 1406279913 [updatetime] => 1406279913 [name] => [lang] => 1 ) )

 

实例分析:瀑布流

<div>    <ul>         <!--第一列-->         <li></li>        <!--第二列-->         <li></li>        <!--第三列-->         <li></li>         <!--第四列-->         <li></li>    </ul></div>                    

html代码

<!-- screeenA瀑布流 -->    <div class="screenA">        <ul>            <li>                <volist name="arr_0" id="vo">                 <div class="ka">                    <div class="im">                        <img src=http://www.mamicode.com/"{$vo.thumb}" />                    </div>                    <div class="text"><a href=http://www.mamicode.com/"{$vo.url}">{$vo.title}</a></div>                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>                    <div class="line"></div>                    <div class="ask">                        <div class="ask_left"><img src=http://www.mamicode.com/"../Public/images/ico_03.jpg" /></div>                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>                        <div class="clear"></div>                    </div>                </div>                </volist>             </li>            <li>                <volist name="arr_1" id="vo">                 <div class="ka">                    <div class="im">                        <img src=http://www.mamicode.com/"{$vo.thumb}" />                    </div>                    <div class="text"><a href=http://www.mamicode.com/"{$vo.url}">{$vo.title}</a></div>                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>                    <div class="line"></div>                    <div class="ask">                        <div class="ask_left"><img src=http://www.mamicode.com/"../Public/images/ico_03.jpg" /></div>                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>                        <div class="clear"></div>                    </div>                </div>                </volist>             </li>            <li>                <volist name="arr_2" id="vo">                 <div class="ka">                    <div class="im">                        <img src=http://www.mamicode.com/"{$vo.thumb}" />                    </div>                    <div class="text"><a href=http://www.mamicode.com/"{$vo.url}">{$vo.title}</a></div>                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>                    <div class="line"></div>                    <div class="ask">                        <div class="ask_left"><img src=http://www.mamicode.com/"../Public/images/ico_03.jpg" /></div>                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>                        <div class="clear"></div>                    </div>                </div>                </volist>             </li>            <li>                <volist name="arr_3" id="vo">                 <div class="ka">                    <div class="im">                        <img src=http://www.mamicode.com/"{$vo.thumb}" />                    </div>                    <div class="text"><a href=http://www.mamicode.com/"{$vo.url}">{$vo.title}</a></div>                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>                    <div class="line"></div>                    <div class="ask">                        <div class="ask_left"><img src=http://www.mamicode.com/"../Public/images/ico_03.jpg" /></div>                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>                        <div class="clear"></div>                    </div>                </div>                </volist>             </li>            <div class="clear"></div>        </ul>    </div>    <!-- screeenA瀑布流End -->

php代码

class IndexAction extends BaseAction{    public function index()    {                $m = M(article);        $res = $m->select();        //print_r($item);                $arr[0] = $this->falls($res,0);        $arr[1] = $this->falls($res,1);        $arr[2] = $this->falls($res,2);        $arr[3] = $this->falls($res,3);                                $this->assign(bcid,0);//顶级栏目         $this->assign(arr_0,$this->falls($res,0));        $this->assign(arr_1,$this->falls($res,1));        $this->assign(arr_2,$this->falls($res,2));        $this->assign(arr_3,$this->falls($res,3));        $this->assign(ishome,home);        $this->display();    }        public function falls($arr,$v){        foreach($arr as $k=>$val){            for($i=0;$i<4;$i++){                $a = $i*4+$v;                if($k==$a){                    $item[] = $val;                    }                }        }        return $item;            }     }