如何在PHP中按主日期键和次字母数字排序
我有一个2D数组,看起来有点像这样:如何在PHP中按主日期键和次字母数字排序,php,mongodb,sorting,date,Php,Mongodb,Sorting,Date,我有一个2D数组,看起来有点像这样: [0] => Array( [date] => 23-01-2017 [name] => bbb [othertext] => text2 ) [1] => Array( [date] => 23-01-2017 [name] => aaa [othertext] => text3 ) [2] => Array( [date] => 24-01
[0] => Array(
[date] => 23-01-2017
[name] => bbb
[othertext] => text2
)
[1] => Array(
[date] => 23-01-2017
[name] => aaa
[othertext] => text3
)
[2] => Array(
[date] => 24-01-2017
[name] => aaa
[othertext] => text1
)
注意:这个问题没有标记为MySQL,使用的数据库是MongoDB,排序类型为
'date'=>'asc'
目前,这是从我的数据库返回的,按日期排序,但不考虑name属性。现在我想按date
对其进行排序,对于具有相同日期的条目,则按name
进行排序
我目前的方法是对数据运行数组\u multisort
:
array_multisort(
$array['date'], SORT_STRING,
$array['name'], SORT_STRING,
$arrayCopy //<--This copy of the array has the full datetime object
);
array\u multisort(
$array['date'],排序字符串,
$array['name'],排序字符串,
数据库查询中的$arrayCopy/:
SELECT * from `your_table_name` order by date asc, name asc;
也许在mongodb中:
$cursor->sort(array('date' => 1, 'name' => 1));
见:
以后无需在php中执行此操作。在数据库查询中:
SELECT * from `your_table_name` order by date asc, name asc;
也许在mongodb中:
$cursor->sort(array('date' => 1, 'name' => 1));
见:
以后无需在php中执行此操作。我强烈建议通过数据库执行此操作
但如果您必须使用usort或试图了解其工作原理:
$arr = [
[
'date' => '23-01-2017',
'name' => 'bbb',
],
[
'date' => '23-01-2017',
'name' => 'aaa',
],
[
'date' => '24-01-2017',
'name' => 'aaa',
],
];
function cmp($a, $b)
{
$aDate = DateTime::createFromFormat('d-m-Y', $a['date']);
$bDate = DateTime::createFromFormat('d-m-Y', $b['date']);
if ($aDate == $bDate) {
if ($a['name'] == $b['name']) {
return 0;
}
return ($a['name'] < $b['name']) ? -1 : 1;
}
return ($aDate < $bDate) ? -1 : 1;
}
usort($arr, "cmp");
print_r($arr);
我强烈建议通过数据库进行此操作
但如果您必须使用usort或试图了解其工作原理:
$arr = [
[
'date' => '23-01-2017',
'name' => 'bbb',
],
[
'date' => '23-01-2017',
'name' => 'aaa',
],
[
'date' => '24-01-2017',
'name' => 'aaa',
],
];
function cmp($a, $b)
{
$aDate = DateTime::createFromFormat('d-m-Y', $a['date']);
$bDate = DateTime::createFromFormat('d-m-Y', $b['date']);
if ($aDate == $bDate) {
if ($a['name'] == $b['name']) {
return 0;
}
return ($a['name'] < $b['name']) ? -1 : 1;
}
return ($aDate < $bDate) ? -1 : 1;
}
usort($arr, "cmp");
print_r($arr);
使用usort功能,您可以执行以下操作:
$foo = array(
0 => array(
"date" => "23-01-2017",
"name" => "bbb",
"othertext" => "text2"
),
1 => array(
"date" => "23-01-2017",
"name" => "aaa",
"othertext" => "text3"
),
2 => array(
"date" => "24-01-2017",
"name" => "aaa",
"othertext" => "text1"
)
);
usort($foo, function($a, $b)
{
return $a["date"] === $b["date"] ? strcmp($a["name"], $b["name"]) : strcmp(strtotime($a["date"]), strtotime($b["date"]));
});
var_dump($foo);
使用usort功能,您可以执行以下操作:
$foo = array(
0 => array(
"date" => "23-01-2017",
"name" => "bbb",
"othertext" => "text2"
),
1 => array(
"date" => "23-01-2017",
"name" => "aaa",
"othertext" => "text3"
),
2 => array(
"date" => "24-01-2017",
"name" => "aaa",
"othertext" => "text1"
)
);
usort($foo, function($a, $b)
{
return $a["date"] === $b["date"] ? strcmp($a["name"], $b["name"]) : strcmp(strtotime($a["date"]), strtotime($b["date"]));
});
var_dump($foo);
简单地说,在您的sql查询中添加此排序,按日期排序ASC,名称ASC
@Ayaou此问题未标记为MySQL,它使用MongoDB数据库-我已扩展了此问题以指定此项。:)我的坏:(这是相同的原则,只需在排序查询中添加第二个参数,您就很好:)简单,在您的sql查询中添加此排序,按日期排序ASC,名称ASC@Ayaou此问题未标记为MySQL,它使用MongoDB数据库-我已扩展问题以指定此项。:)我的缺点:(这是同样的原理,只需在排序查询中添加第二个参数,你就很好了:)这个问题没有标记为MySQL,它使用MongoDB数据库-我已经扩展了这个问题来指定它。:)更新了我的答案此问题未标记为MySQL,它使用MongoDB数据库-我已扩展此问题以指定此问题。:)更新了我的答案