>백엔드 개발 >PHP 튜토리얼 >如何运用PHP数组排序函数array_multisort进行表格排序_PHP教程

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

WBOY
WBOY원래의
2016-07-15 13:29:05969검색

我们今天为大家带来的是一个函数的原形是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对表格排序的具体代码编写。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446380.htmlTechArticle我们今天为大家带来的是一个 函数的原形是array_multisort($sortKeyArray,$ascOrDesc,$sortArray),PHP数组排序函数array_multisort中的第一个参数是为了保...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.