PHP Programming for multidimensional arrays to sort by a key value

Source: Internet
Author: User

1.array_multisort()function to sort multiple arrays or multidimensional arrays.//对数组$hotcat按照count键值大小降序进行排序;$hotcat =array( array(‘1501‘=>array(‘catid‘=>‘1546‘,‘catname‘=>‘数组排序 一级‘,‘count‘=>‘588‘)), array(‘1501‘=>array(‘catid‘=>‘1546‘,‘catname‘=>‘数组排序二级‘,‘count‘=>‘588‘)), array(‘1501‘=>array(‘catid‘=>‘1546‘,‘catname‘=>‘数组排序 三级‘,‘count‘=>‘588‘)));//提取列数组;foreach ($hotcat as $key => $val) { $tmp[$key] = $row[‘username‘];}array_multisort($tmp,SORT_DESC,$hotcat);此处对数组进行降序排列;SORT_DESC按降序排列2. Custom Methodsarray_sort()To sort//指定数组以$keys键值排序function array_sort($array,$keys,$type=‘asc‘){//$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序$keysvalue = $new_array = array();foreach ($array as $k=>$v){$keysvalue[$k] = $v[$keys];}if($type == ‘asc‘){asort($keysvalue);}else{arsort($keysvalue);}reset($keysvalue);foreach ($keysvalue as $k=>$v){$new_array[$k] = $array[$k];}return $new_array;}Transferred from: https://www.jb51.net/article/112285.htm (thanks for sharing)

PHP Programming for multidimensional arrays to sort by a key value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.