I have a database table which gives me the following result:
我有一个数据库表,它给我以下结果:
array(8) {
["link_id"]=>
string(2) "20"
["link_url"]=>
string(56) "http://url.of/website"
["link_name"]=>
string(34) "Website title"
["link_target"]=>
string(0) ""
["link_description"]=>
string(0) ""
["link_updated"]=>
string(19) "2009-05-24 16:51:04"
["taxonomy_id"]=>
string(2) "36"
["term_id"]=>
string(2) "34"
["category_name"]=>
string(15) "Link category"
}
I want to sort many of these arrays in to one multidimensional array, based on the category_name key, and then sorted by the link_updated key.
我想基于category_name键将许多这些数组排序到一个多维数组中,然后按link_updated键排序。
So I eventually want it to look like this:
所以我最终希望它看起来像这样:
array(2) {
["First category"]=>
array(2) {
["link_name"]=>
string(11) "Newest link"
["link_updated"]=>
string(19) "2009-05-24 16:51:24"
}
["Second category"]=>
array(2) {
["link_name"]=>
string(10) "Older link"
["link_updated"]=>
string(19) "2009-05-20 05:32:56"
}
}
I have no idea how to do this, but I think I have to make my own sorting method (usort())?
我不知道怎么做,但我想我必须自己做排序方法(usort())?
EDIT: I want to show 5 links in each category.
编辑:我想在每个类别中显示5个链接。
3 个解决方案
#1
Using usort() you can sort your arrays in any way you want:
使用usort(),您可以以任何方式对数组进行排序:
function sort_crazy_way($a, $b){
// do your business.
}
usort($array, 'sort_crazy_way');
After you get it sorted, you can create the last array in another for loop.
对它进行排序后,可以在另一个for循环中创建最后一个数组。
From the PHP manual:
从PHP手册:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
如果第一个参数被认为分别小于,等于或大于第二个参数,则比较函数必须返回小于,等于或大于零的整数。
So, your comparison function should be something like:
所以,你的比较函数应该是这样的:
function sort_crazy_way($a, $b){
$sorted_by_category = strcmp($a['category_name'], $b['category_name']);
if($sorted_by_category){
return $sorted_by_category;
}
// If we're here, category_name is the same. Compare link_updated.
$time_diff = strtotime($a['link_updated']) - strtotime($b['link_updated']);
return $time_diff;
}
#3
I solved it myself, by using the following code:
我通过使用以下代码自己解决了这个问题:
foreach ($bookmarks as $b)
{
$category[$b["category_name"]][] = array(
"link_id" => $b["link_id"],
"link_url" => $b["link_url"],
"link_name" => $b["link_name"],
"link_target" => $b["link_target"],
"link_description" => $b["link_description"],
"link_updated" => $b["link_updated"],
"taxonomy_id" => $b["taxonomy_id"],
"term_id" => $b["term_id"],
"category_name" => $b["category_name"]
);
}
This creates an array of the category name, and puts all subarrays in the right parent array (based on category). The sorting after time the link is updated is being made in the SQL query.
这将创建一个类别名称数组,并将所有子数组放在右父数组中(基于类别)。在SQL查询中进行链接更新后的排序。
#1
Using usort() you can sort your arrays in any way you want:
使用usort(),您可以以任何方式对数组进行排序:
function sort_crazy_way($a, $b){
// do your business.
}
usort($array, 'sort_crazy_way');
After you get it sorted, you can create the last array in another for loop.
对它进行排序后,可以在另一个for循环中创建最后一个数组。
From the PHP manual:
从PHP手册:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
如果第一个参数被认为分别小于,等于或大于第二个参数,则比较函数必须返回小于,等于或大于零的整数。
So, your comparison function should be something like:
所以,你的比较函数应该是这样的:
function sort_crazy_way($a, $b){
$sorted_by_category = strcmp($a['category_name'], $b['category_name']);
if($sorted_by_category){
return $sorted_by_category;
}
// If we're here, category_name is the same. Compare link_updated.
$time_diff = strtotime($a['link_updated']) - strtotime($b['link_updated']);
return $time_diff;
}
#2
array_multisort should do the trick here - it's quite powerful.
array_multisort应该在这里做 - 它非常强大。
#3
I solved it myself, by using the following code:
我通过使用以下代码自己解决了这个问题:
foreach ($bookmarks as $b)
{
$category[$b["category_name"]][] = array(
"link_id" => $b["link_id"],
"link_url" => $b["link_url"],
"link_name" => $b["link_name"],
"link_target" => $b["link_target"],
"link_description" => $b["link_description"],
"link_updated" => $b["link_updated"],
"taxonomy_id" => $b["taxonomy_id"],
"term_id" => $b["term_id"],
"category_name" => $b["category_name"]
);
}
This creates an array of the category name, and puts all subarrays in the right parent array (based on category). The sorting after time the link is updated is being made in the SQL query.
这将创建一个类别名称数组,并将所有子数组放在右父数组中(基于类别)。在SQL查询中进行链接更新后的排序。