简体   繁体   English

php array_multisort()通过2个字段

[英]php array_multisort() by 2 field

I use array_multisort() function to sort an array by a field value. 我使用array_multisort()函数按字段值对数组进行排序。 what I need is to sort it by 2 field values, by date and by time. 我需要按日期和时间按2个字段值对其进行排序。 here is the structure of the array: 这是数组的结构:

Array
(
    [0] => Array
        (
            [id_art] => 5292
            [free_art] => 0
            [apero_art] => 0
            [name_cat] => Teatro
            [date_dat] => 2010-11-24
            [date2_dat] => 0000-00-00
            [name_spa] => Cinema Teatro
            [title_int] => Il piacere dell'onestà 
            [id_cat] => 2
            [time_tim] => 20:30:00
            [intro_int] => Una produzione Teatro Eliseo di Roma - ChiassoCultura 
            [image_art] => noimage.png
        )

    [1] => Array
        (
            [id_art] => 4983
            [free_art] => 0
            [apero_art] => 0
            [name_cat] => Cinema
            [date_dat] => 2011-04-20
            [date2_dat] => 2011-04-20
            [name_spa] => Cinema Morettina
            [title_int] => Inland Empire
            [id_cat] => 1
            [time_tim] => 20:30:00
            [intro_int] => Rassegna dedicata a David Lynch
            [image_art] => noimage.png
        )

    [2] => Array
        (
            [id_art] => 4983
            [free_art] => 0
            [apero_art] => 0
            [name_cat] => Cinema
            [date_dat] => 2011-04-22
            [date2_dat] => 2011-04-22
            [name_spa] => Cinema Iride
            [title_int] => Inland Empire
            [id_cat] => 1
            [time_tim] => 17:00:00
            [intro_int] => Rassegna dedicata a David Lynch
            [image_art] => noimage.png
        )....

What I do now is: 我现在要做的是:

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }
    array_multisort($sort_col, $dir, $arr); 
}

How can I easily make in one time the double sort? 如何一次轻松实现双重排序?

Thanks. 谢谢。

If I understand what you need to do, you can use usort: 如果我了解您需要做什么,可以使用usort:

usort($array, function($a, $b) {
    if ($a['date_cat'] > $b['date_cat']) return 1;
    if ($a['date_cat'] < $b['date_cat']) return -1;
    if ($a['time_tim'] > $b['time_tim']) return 1;
    if ($a['time_tim'] < $b['time_tim']) return -1;        
    return 0;
});

The way your function works it a bit twisty, to me. 对我来说,您的功能运作方式有些曲折。 You extract a column of your array to create an array to sort linked to the original array. 您提取数组的一列以创建要排序的数组,该数组链接到原始数组。 It took several readings and a php manual check to understand your code (too bad), you should have (to me) detach the creation of the index array into another function prior than calling the sort. 花了几读和一次php手动检查才能理解您的代码(太糟糕了),(对我而言)您应该(在我看来)在调用排序之前将索引数组的创建分离到另一个函数中。

If you need to have dynamic colums selection maybe you can leverage on closures (PHP 5.3+ ?): 如果需要动态选择列,则可以利用闭包(PHP 5.3+?):

function your_sort($orderby, $array) {
    return usort($array, function ($a, $b) use ($orderby) {
        foreach($orderby as $field) {
            if ($a[$field] > $b[$field]) return 1;
            if ($a[$field] < $b[$field]) return -1;
        }
        return 0;
    });
}

And then you can call the function like this: 然后您可以像下面这样调用函数:

your_sort(array('date_cat','time_tim'), $array);

Usign some code from php.net comment and other adjustment, I wrote a function that accept these argument: 从php.net注释和其他调整中获得一些代码,我编写了一个接受以下参数的函数:

  1. $a = array $ a =数组
  2. $orderby = an order string written in sql language $ orderby =用sql语言编写的订单字符串
  3. $children_key = name of the eventual column where eventual children node are saved $ children_key =保存最终子节点的最终列的名称

Here the function: 这里的功能:

function array_multiorderby( $data, $orderby, $children_key=false )
{
    // parsing orderby
    $args = array();
    $x = explode( ' ', str_replace( ',', ' ', $orderby ) );
    foreach( $x as $item ) 
    {
        $item = trim( $item );
        if( $item=='' ) continue;
        if( strtolower($item)=='asc' ) $item = SORT_ASC;
        else if( strtolower($item)=='desc' ) $item = SORT_DESC;
        $args[] = $item;
    }

    // order
    foreach ($args as $n => $field) 
    {
        if (is_string($field)) 
        {
            $tmp = array();
            foreach ($data as $key => $row)
            $tmp[$key] = $row[$field];
            $args[$n] = $tmp;
        }
    }
    $args[] = &$data;
    call_user_func_array('array_multisort', $args);
    $data = array_pop($args);

    // order children
    if( $children_key )
    {
        foreach( $data as $k=>$v ) if( is_array($v[$children_key]) ) $data[$k][$children_key] = array_multiorderby( $v[$children_key], $children_key, $orderby );
    }

    // return
    return $data;
}

Application to your specific case: 适用于您的特定情况:

$array = array_multiorderby( $array, "date_dat DESC, time_tim DESC" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM