0

使用usort(),是否可以对也包含整数值的字符串进行排序?

例如,采用包含电子邮件地址(和其他数据)的对象数组 -

$invitees = Array(
    [0] => Array(
        'email' => 'test11@testing.com'
    ),
    [1] => Array(
        'email' => 'test2@testing.com'
    ),
    [2] => Array(
        'email' => 'test1@testing.com'
    )
);

使用以下代码将数组元素作为一个简单的字符串进行比较 -

/** Sort the data (if the sort key is defined) */
if(!empty($_REQUEST['orderby'])) :
    usort($emails, array(&$this, '_order_callback'));
endif;

function _order_callback($item_a, $item_b){

    /** Grab 'orderby', which must have been set for this function to be called */
    $orderby = $_REQUEST['orderby'];

    /** If no 'order' is not set, default to ASC */
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'ASC';

    $result = strcmp($item_a[$orderby], $item_b[$orderby]);

    return (strtoupper($order) === 'ASC') ? $result : -$result; //Send final sort direction to usort

}

结果按以下顺序交付 -

[0] - 'test11@testing.com'
[2] - 'test1@testing.com'
[1] - 'test2@testing.com'

我想要这个订单的地方 -

[2] - 'test1@testing.com'
[1] - 'test2@testing.com'
[0] - 'test11@testing.com'

这可能usort()吗?谢谢。


编辑

现在我知道存在natsort()(感谢以下评论/答案),我能够找到并尝试这个 -

$result = ($item_a[$orderby] > $item_b[$orderby] ? 1 : ($item_a[$orderby] < $item_b[$orderby] ? -1 : 0));

我将该比较添加到我的_order_callback()函数(if $orderby === email)中,它很接近,但按 order 排序11, 12, 13, 14, 1, 2, 3, 4

4

3 回答 3

2

使用strnatcmp()在您的 usort() 回调中进行比较

$email1 = new StdClass;
$email1->email = 'test11@testing.com';
$email2 = new StdClass;
$email2->email = 'test1@testing.com';
$email3 = new StdClass;
$email3->email = 'test2@testing.com';
$email4 = new StdClass;
$email4->email = 'test12@testing.com';
$email5 = new StdClass;
$email5->email = 'test21@testing.com';
$email6 = new StdClass;
$email6->email = 'test3@testing.com';

$invitees = array(
    $email1,
    $email2,
    $email3,
    $email4,
    $email5,
    $email6,
);


usort($invitees, '_order_callback');

function _order_callback($item_a, $item_b){
    return strnatcmp($item_a->email, $item_b->email);
}

var_dump($invitees);
于 2013-08-08T17:50:10.793 回答
1

正如马克贝克所说,natsort这就是您要寻找的。

$emails = array(
    'test2@testing.com',
    'test11@testing.com',
    'test1@testing.com'
);

natsort( $emails );
于 2013-08-08T16:01:29.780 回答
0

感谢@MarkBaker、@MajorCaiger 和@hdvianna,这是我正在使用的最终订单回调函数。

此函数考虑了来自多维数组的多个排序标准。

/** Sort the data (if the sort key is defined) */
if(!empty($_REQUEST['orderby'])) :
    usort($invitees[$status], array(&$this, '_order_callback'));
endif;

/**
 * Callback function to order event invitees
 *
function _order_callback($item_a, $item_b){

    /** Grab 'orderby', which must have been set for this function to be called */
    $orderby = $_REQUEST['orderby'];

    /** If no 'order' is not set, default to ASC */
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'ASC';

    switch($orderby) :

        case 'ID' : // ID is unique, so just sort by ID
            $result = strnatcmp($item_a[$orderby], $item_b[$orderby]);
            break;

        case 'email' :
            $result = strnatcasecmp($item_a[$orderby], $item_b[$orderby]);
            if($result === 0) :
                $result = strcmp($item_a['first_name'], $item_b['first_name']);
            endif;
            if($result === 0) :
                $result = strcmp($item_a['surname'], $item_b['surname']);
            endif;
            break;

        case 'name' :
            $result = strcmp($item_a['first_name'], $item_b['first_name']); // Explicitly declare 'first_name' here as $orderby is actuualy 'name', which is a constructed field for display
            if($result === 0) :
                $result = strcmp($item_a['surname'], $item_b['surname']);
            endif;
            if($result === 0) :
                $result = strnatcasecmp($item_a['email'], $item_b['email']);
            endif;
            break;

        default : // 'custom' and 'town'
            $result = strcmp($item_a[$orderby], $item_b[$orderby]);
            break;

    endswitch;

    return (strtoupper($order) === 'ASC') ? $result : -$result; //Send final sort direction to usort

}
于 2013-08-09T09:26:52.497 回答