根据不同的值对数组进行排序

时间:2014-11-10 10:46:11

标签: php arrays sorting

让我们假设你有一系列的日子和开放时间这样的商店:

Array
(
    [monday] => Array
        (
            [day] => Monday
            [isopen] => true
            [open] => 10:00
            [close] => 17:00
        )

    [tuesday] => Array
        (
            [day] => Tuesday
            [isopen] => true
            [open] => 10:00
            [close] => 17:00
        )

    [wednesday] => Array
        (
            [day] => Wednesday
            [isopen] => true
            [open] => 10:00
            [close] => 17:00
        )

    [thursday] => Array
        (
            [day] => Thursday
            [isopen] => true
            [open] => 10:00
            [close] => 17:00
        )

    [friday] => Array
        (
            [day] => Friday
            [isopen] => true
            [open] => 10:00
            [close] => 17:00
        )

    [saturday] => Array
        (
            [day] => Saturday
            [isopen] => false
            [open] => 11:00
            [close] => 14:00
        )

    [sunday] => Array
        (
            [day] => Sunday
            [isopen] => false
            [open] => 01:00
            [close] => 01:00
        )

)

现在,我想像这样对这些婴儿进行排序:

monday - friday: 10:00-17:00
saturday-sunday: closed

以免星期三关闭16:00我希望这个输出:

monday-tuesday: 10:00-17:00
wednesday: 10:00-16:00
thursday-friday: 10:00-17:00
saturday-sunday: closed

也就是说,我想对它们进行排序,但仍然保持从上到下的顺序作为主要排序。那么,你会怎么做?将它们全部循环并在内存中保留数小时并比较天数是一种选择,但是有没有更好的解决方案?

编辑: 请求了一个实际的数组,这里是:

$days = array(
            'monday' => array(
                    'day' => 'Monday',
                    'isopen' => true,
                    'open' => '10:00',
                    'close' => '17:00',
                ),
            'tuesday' => array(
                    'day' => 'Tuesday',
                    'isopen' => true,
                    'open' => '10:00',
                    'close' => '17:00',
                ),
            'wednesday' => array(
                    'day' => 'Wednesday',
                    'isopen' => true,
                    'open' => '10:00',
                    'close' => '17:00',
                ),
            'thursday' => array(
                    'day' => 'Thursday',
                    'isopen' => true,
                    'open' => '10:00',
                    'close' => '17:00',
                ),
            'friday' => array(
                    'day' => 'Friday',
                    'isopen' => true,
                    'open' => '10:00',
                    'close' => '17:00',
                ),
            'saturday' => array(
                    'day' => 'Saturday',
                    'isopen' => false,
                    'open' => '10:00', //Not used since isopen = false
                    'close' => '17:00', //Not used since isopen = false
                ),
            'sunday' => array(
                    'day' => 'Sunday',
                    'isopen' => false,
                    'open' => '10:00', //Not used since isopen = false
                    'close' => '17:00', //Not used since isopen = false
                )
        );

2 个答案:

答案 0 :(得分:1)

这很棘手,但不是那么不可能(并且要注意它是一种合并而不是排序):

    $sorted = array();
    foreach($days as $k=>$v){

        $current = array(
            'open'=>$v['open'],
            'close'=>$v['close'],
            'isopen'=>$v['isopen'],
        );              

        if (empty($sorted) || $previous != $current ) {
            $sorted[] = array('firstDay'=>$k,'open'=>$v['open'],'close'=>$v['close']);
        } else
            $sorted[count($sorted)-1]['lastDay'] = $k;


        $previous = $current;   
    }

    print_r($sorted);

答案 1 :(得分:0)

类似的东西:

$newArray = array();
$dayCount = -1;
foreach ($days as $day) {
    if ( ($dayCount < 0) || 
         ($day['isopen'] != $newArray[$dayCount]['isopen']) || 
         ($day['open'] != $newArray[$dayCount]['open']) || 
         ($day['close'] != $newArray[$dayCount]['close']) ) {
        $newArray[++$dayCount] = array_merge(
            $day,
            array('dayTo' => '')
        );
    } else {
        $newArray[$dayCount]['dayTo'] = $day['day'];
    }
}
var_dump($newArray);

修改

从发布的初始数组中,我们得出:

array(2) {
  [0]=>
  array(5) {
    ["day"]=>
    string(6) "Monday"
    ["isopen"]=>
    bool(true)
    ["open"]=>
    string(5) "10:00"
    ["close"]=>
    string(5) "17:00"
    ["dayTo"]=>
    string(6) "Friday"
  }
  [1]=>
  array(5) {
    ["day"]=>
    string(8) "Saturday"
    ["isopen"]=>
    bool(false)
    ["open"]=>
    string(5) "10:00"
    ["close"]=>
    string(5) "17:00"
    ["dayTo"]=>
    string(6) "Sunday"
  }
}

如果您在“关闭”日有不匹配的openclose值,那么将if条件更改为

if ( ($dayCount < 0) || 
     ($day['isopen'] != $newArray[$dayCount]['isopen']) || 
     ($day['isopen'] && (($day['open'] != $newArray[$dayCount]['open']) || 
                         ($day['close'] != $newArray[$dayCount]['close']) ))) {

可能有帮助