使用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()
函数(如果$orderby === email
),并且它已接近,但按照11, 12, 13, 14, 1, 2, 3, 4
的顺序进行排序。
答案 0 :(得分: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);
答案 1 :(得分:1)
正如Mark Baker所说,natsort
正是你要找的。 p>
$emails = array(
'test2@testing.com',
'test11@testing.com',
'test1@testing.com'
);
natsort( $emails );
答案 2 :(得分: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
}