search
Homephp教程php手册PHP中二维数组的排序方法

PHP中二维数组的排序方法

Jun 06, 2016 pm 07:57 PM
linuxphpsortarraymethodCommunityEnter

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 本文介绍的是从 BugFree 摘录来的二维数组排序函数,可以实现类似 MySQL 的 ORDER BY 效果,当数组不是从数据库取得时会有特殊应用。 ?php // 说明:PHP中二维数组的排序方法 // 整理:http://www.xk

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

    本文介绍的是从 BugFree 摘录来的二维数组排序函数,可以实现类似 MySQL 的 ORDER BY 效果,当数组不是从数据库取得时会有特殊应用。

   

    // 说明:PHP中二维数组的排序方法

    // 整理:http://www.xker.com

    /**

    * @package     BugFree

    * @version     $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $

    *

    *

    * Sort an two-dimension array by some level two items use array_multisort() function.

    *

    * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)

    * @author                      Chunsheng Wang

    * @param  array   $ArrayData   the array to sort.

    * @param  string  $KeyName1    the first item to sort by.

    * @param  string  $SortOrder1  the order to sort by("SORT_ASC"|"SORT_DESC")

    * @param  string  $SortType1   the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")

    * @return array                sorted array.

    */

    function sysSortArray

    (

    $ArrayData,

    $KeyName1,

    $SortOrder1 =

    "SORT_ASC",

    $SortType1 =

    "SORT_REGULAR"

    )

    {

    if

    (!

    is_array

    (

    $ArrayData

    )

    )

    {

    return

    $ArrayData;

    }

    // Get args number.

    $ArgCount =

    func_num_args

    (

    );

    // Get keys to sort by and put them to SortRule array.

    for

    (

    $I =

    1;

    $I

    $ArgCount;

    $I ++

    )

    {

    $Arg =

    func_get_arg

    (

    $I

    );

    if

    (!

    eregi

    (

    "SORT",

    $Arg

    )

    )

    {

    $KeyNameList

    [

    ] =

    $Arg;

    $SortRule

    [

    ]    =

    '$'.

    $Arg;

    }

    else

    {

    $SortRule

    [

    ]    =

    $Arg;

    }

    }

    // Get the values according to the keys and put them to array.

    foreach

    (

    $ArrayData

    AS

    $Key =>

    $Info

    )

    {

    foreach

    (

    $KeyNameList

    AS

    $KeyName

    )

    {

    $

    {

    $KeyName

    }

    [

    $Key

    ] =

    $Info

    [

    $KeyName

    ];

    }

    }

    // Create the eval string and eval it.

    $EvalString =

    'array_multisort('.

    join

    (

    ",",

    $SortRule

    )。

    ',$ArrayData);';

    eval

    (

    $EvalString

    );

    return

    $ArrayData;

    }

    //################# 示例 #################

    $arr =

    array

    (

    array

    (

    'name'        =>

    '学习',

    'size'        =>

    '1235',

    'type'        =>

    'jpe',

    'time'        =>

    '1921-11-13',

    'class'        =>

    'dd',

    ),

    array

    (

    'name'        =>

    '中国功夫',

    'size'        =>

    '153',

    'type'        =>

    'jpe',

    'time'        =>

    '2005-11-13',

    'class'        =>

    'jj',

    ),

    array

    (

    'name'        =>

    '编程',

    'size'        =>

    '35',

    'type'        =>

    'gif',

    'time'        =>

    '1997-11-13',

    'class'        =>

    'dd',

    ),

    array

    (

    'name'        =>

    '中国功夫',

    'size'        =>

    '65',

    'type'        =>

    'jpe',

    'time'        =>

    '1925-02-13',

    'class'        =>

    'yy',

    ),

    array

    (

    'name'        =>

    '中国功夫',

    'size'        =>

    '5',

    'type'        =>

    'icon',

    'time'        =>

    '1967-12-13',

    'class'        =>

    'rr',

    ),

    );

    print_r

    (

    $arr

    );

    //注意:按照数字方式排序时 153 比 65 小

    $temp = sysSortArray

    (

    $arr,

    "name",

    "SORT_ASC",

    "type",

    "SORT_DESC",

    "size",

    "SORT_ASC",

    "SORT_STRING"

    );

    print_r

    (

    $temp

    );

    ?>

PHP中二维数组的排序方法

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor