search
Homephp教程php手册PHP二维数组排序函数

PHP二维数组排序函数 PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义。 以下函数是对一个给定的二维数组按照指定的键值进行排序,先看函数定义: function array_sort($arr,$keys,$type='asc'){ $keysvalue = $ne

PHP二维数组排序函数

PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义。
以下函数是对一个给定的二维数组按照指定的键值进行排序,先看函数定义:

function array_sort($arr,$keys,$type='asc'){ 
    $keysvalue = $new_array = array();
    foreach ($arr 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] = $arr[$k];
    }
    return $new_array; 
} 

它可以对二维数组按照指定的键值进行排序,也可以指定升序或降序排序法(默认为升序),用法示例:

$array = array(
    array('name'=>'手机','brand'=>'诺基亚','price'=>1050),
    array('name'=>'笔记本电脑','brand'=>'lenovo','price'=>4300),
    array('name'=>'剃须刀','brand'=>'飞利浦','price'=>3100),
    array('name'=>'跑步机','brand'=>'三和松石','price'=>4900),
    array('name'=>'手表','brand'=>'卡西欧','price'=>960),
    array('name'=>'液晶电视','brand'=>'索尼','price'=>6299),
    array('name'=>'激光打印机','brand'=>'惠普','price'=>1200)
);
$ShoppingList = array_sort($array,'price');
print_r($ShoppingList);

上面是对$array这个二维数组按照'price'从低到高的排序。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool