首页 > php教程 > php手册 > 正文

如何运用PHP数组排序函数array_multisort进行表格排序

WBOY
发布: 2016-06-13 11:06:23
原创
914 人浏览过

我们今天为大家带来的是一个函数的原形是array_multisort($sortKeyArray,$ascOrDesc,$sortArray),PHP数组排序函数array_multisort中的第一个参数是为了保持数组键值的对应关系需要构建的排序列数组,第二个参数是预定义的常量,SORT_ASC - 按照上升顺序排序,SORT_DESC - 按照下降顺序排序,第三个参数就是所要被排序的数组。还有一个可缺省的参数是排序的数据类型,这里略过。看一下下面的例子就会明了。

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span> </span></li>
<li class="alt"><span>class Storage  </span></li>
<li class=""><span>   {  </span></li>
<li class="alt"><span>         function getSellList()  </span></li>
<li class=""><span>       {  </span></li>
<li class="alt"><span>         global $db;  </span></li>
<li class="">
<span>        $db-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>query("set names utf8");  </span>
</li>
<li class="alt">
<span>       $db-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>query("select * from sold_record");  </span>
</li>
<li class="">
<span>       while ($</span><span class="attribute"><font color="#ff0000">row</font></span><span>=$db-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get_array())  </span>
</li>
<li class="alt"><span>      {  </span></li>
<li class=""><span>          $array[]=$row;  </span></li>
<li class="alt"><span>        }  </span></li>
<li class="">
<span>        $db-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>free();  </span>
</li>
<li class="alt"><span>        return $array;  </span></li>
<li class=""><span>      }   </span></li>
<li class="alt"><span>  }  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">storage</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Storage();  </span>
</li>
<li class="">
<span>    $</span><span class="attribute"><font color="#ff0000">sellList</font></span><span>=$storage-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getSellList();  </span>
</li>
<li class="alt">
<span>    foreach ($sellList as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $row)  </span>
</li>
<li class=""><span>     {  </span></li>
<li class="alt"><span>            $cust[$key]  = $row['customer_id'];  </span></li>
<li class=""><span>   $prod[$key] = $row['product'];  </span></li>
<li class="alt"><span>            $pty[$key] = $row['ptype'];  </span></li>
<li class=""><span>   $sdt[$key]=$row['sell_date'];  </span></li>
<li class="alt"><span>        }  </span></li>
<li class="">
<span>  $</span><span class="attribute"><font color="#ff0000">asdes</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"yes"</font></span><span>;  </span>
</li>
<li class="alt"><span>  if($_GET[up]=="yes")  </span></li>
<li class=""><span>   {  </span></li>
<li class="alt">
<span>       $</span><span class="attribute"><font color="#ff0000">asdes</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"no"</font></span><span>;  </span>
</li>
<li class="">
<span>       $</span><span class="attribute"><font color="#ff0000">sort</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">SORT_DESC</font></span><span>;  </span>
</li>
<li class="alt"><span>   }  </span></li>
<li class=""><span>   elseif($_GET[up]=="no")  </span></li>
<li class="alt"><span>   {  </span></li>
<li class="">
<span>       $</span><span class="attribute"><font color="#ff0000">asdes</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"yes"</font></span><span>;  </span>
</li>
<li class="alt">
<span>       $</span><span class="attribute"><font color="#ff0000">sort</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">SORT_ASC</font></span><span>;  </span>
</li>
<li class=""><span>      </span></li>
<li class="alt"><span>   }  </span></li>
<li class=""><span>     </span></li>
<li class="alt"><span>        switch ($_GET[order])  </span></li>
<li class=""><span>  {  </span></li>
<li class="alt"><span>      case "cname":  </span></li>
<li class=""><span>   array_multisort($cust,$sort,$sellList);  </span></li>
<li class="alt"><span>   break;  </span></li>
<li class=""><span>   case "product":  </span></li>
<li class="alt"><span>   array_multisort($prod,$sort,$sellList);  </span></li>
<li class=""><span>   break;  </span></li>
<li class="alt"><span>   case "ptype":  </span></li>
<li class=""><span>   array_multisort($pty,$sort,$sellList);  </span></li>
<li class="alt"><span>   break;  </span></li>
<li class=""><span>   case "date":  </span></li>
<li class="alt"><span>   array_multisort($sdt,$sort,$sellList);  </span></li>
<li class=""><span>   break;  </span></li>
<li class="alt"><span>  }  </span></li>
<li class=""><span>      </span></li>
<li class="alt">
<span>   </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
登录后复制

上面这段代码就是PHP数组排序函数array_multisort对表格排序的具体代码编写。


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板