search
Homephp教程php手册PHP二维数组怎么按某个字段排序?

PHP二维数组怎么按某个字段排序?

Jun 13, 2016 am 09:51 AM
phpbutfunctionFieldrighthowdesignationaccording tosortdataarrayhaveof

数据排序有很多现成的函数,但是如果想对指定字段进行排序就没有内置函数了,下面我整理了一些相关的数组排序函数与大家一起交流学习学习。

先不说别的我们直接看二维数组按某个字段排序实例

 代码如下 复制代码
    /**  
     * @author yebihai http://www.bKjia.c0m
     * @desc 按二维数组的某一个字段进行升降排序  
     * @data  
     *      $testData = array(  
                array('price'=>19),  
                array('price'=>121),  
                array('price'=>115),  
                array('price'=>113),  
                array('price'=>112)  
            );  
     */    
    class sortClass{    
        //升序    
        function sortArrayAsc($preData,$sortType='price'){    
            $sortData = array();    
            foreach ($preData as $key_i => $value_i){    
                $price_i = $value_i[$sortType];    
                $min_key = '';    
                $sort_total = count($sortData);    
                foreach ($sortData as $key_j => $value_j){    
                    if($price_i                         $min_key = $key_j+1;    
                        break;    
                    }    
                }    
                if(empty($min_key)){  
                    array_push($sortData, $value_i);     
                }else {    
                    $sortData1 = array_slice($sortData, 0,$min_key-1);     
                    array_push($sortData1, $value_i);    
                    if(($min_key-1)                         $sortData2 = array_slice($sortData, $min_key-1);     
                        foreach ($sortData2 as $value){    
                            array_push($sortData1, $value);    
                        }    
                    }    
                    $sortData = $sortData1;    
                }    
            }    
            return $sortData;    
        }    
        //降序    
        function sortArrayDesc($preData,$sortType='price'){    
            $sortData = array();    
            foreach ($preData as $key_i => $value_i){    
                $price_i = $value_i[$sortType];    
                $min_key = '';    
                $sort_total = count($sortData);    
                foreach ($sortData as $key_j => $value_j){    
                    if($price_i>$value_j[$sortType]){    
                        $min_key = $key_j+1;    
                        break;    
                    }    
                }    
                if(empty($min_key)){    
                    array_push($sortData, $value_i);     
                }else {    
                    $sortData1 = array_slice($sortData, 0,$min_key-1);     
                    array_push($sortData1, $value_i);    
                    if(($min_key-1)                         $sortData2 = array_slice($sortData, $min_key-1);     
                        foreach ($sortData2 as $value){    
                            array_push($sortData1, $value);    
                        }    
                    }    
                    $sortData = $sortData1;    
                }    
            }    
            return $sortData;    
        }    
    }    

后面整理一些关于数组排序的函数

数据排序函数有

•sort() 函数用于对数组单元从低到高进行排序。
•rsort() 函数用于对数组单元从高到低进行排序。
•asort() 函数用于对数组单元从低到高进行排序并保持索引关系。
•arsort() 函数用于对数组单元从高到低进行排序并保持索引关系。
•ksort() 函数用于对数组单元按照键名从低到高进行排序。
•krsort() 函数用于对数组单元按照键名从高到低进行排序。
array_multisort() 函数对多个数组或多维数组进行排序

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment