简体   繁体   English

按键将数组排序为多维数组

[英]Sort arrays into multidimensional array by key

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个链接。

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;
}

array_multisort应该可以解决问题-它非常强大。

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查询中对链接更新后的时间进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM