排序其值为关联数组的数组

时间:2017-01-30 13:41:32

标签: php

我有一个数组,其值是关联数组。我需要根据键对这个数组进行排序;

$list = array (
    array("post_id"=>"2","date"=>"2015","title"=>"title2"),
    array("post_id"=>"4","date"=>"2017","title"=>"title4"),
    array("post_id"=>"3","date"=>"2016","title"=>"title3"),
    array("post_id"=>"1","date"=>"2014","title"=>"title1")
);

例如,通过post_id对此数组进行排序,这意味着将该数组转换为:

$list = array (
    array("post_id"=>"1","date"=>"2014","title"=>"title1"),
    array("post_id"=>"2","date"=>"2015","title"=>"title2"),
    array("post_id"=>"3","date"=>"2016","title"=>"title3"),
    array("post_id"=>"4","date"=>"2017","title"=>"title4")


);

我搜索了这个论坛并找到了这段代码:

function array_sort($array, $on, $order=SORT_ASC){

$new_array = array();
$sortable_array = array();

if (count($array) > 0) {
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            foreach ($v as $k2 => $v2) {
                if ($k2 == $on) {
                    $sortable_array[$k] = $v2;
                }
            }
        } else {
            $sortable_array[$k] = $v;
        }
    }

    switch ($order) {
        case SORT_ASC:
            asort($sortable_array);
            break;
        case SORT_DESC:
            arsort($sortable_array);
            break;
    }

    foreach ($sortable_array as $k => $v) {
        $new_array[$k] = $array[$k];
    }
}

return $new_array;}

但是我无法理解它到底在做什么。 主题链接是: Sort PHP multi-dimensional array based on key?

6 个答案:

答案 0 :(得分:1)

您没有按键排序,因此您找到的示例不会为您效劳。一个简单的usort会做到这一点;

$list = array (
    array("post_id"=>"2","date"=>"2015","title"=>"title2"),
    array("post_id"=>"4","date"=>"2017","title"=>"title4"),
    array("post_id"=>"3","date"=>"2016","title"=>"title3"),
    array("post_id"=>"1","date"=>"2014","title"=>"title1")
);

function sortPosts($a, $b)
{
    if ($a['post_id'] == $b['post_id']) {
        return 0;
    }
    return ($a['post_id'] < $b['post_id']) ? -1 : 1;
}


usort($list, "sortPosts");

这会将您的$lists数组传递到一个函数中并比较每个值 - 您可以看到我们正在比较每个值的['post_id']值。

由于 post_id 是原始数组中的字符串,您可能需要将它们作为整数进行类型转换,但请看看如何进行。

答案 1 :(得分:1)

最优雅的通​​常是将usort()功能与closure结合使用。

看看这个简单的演示:

<?php

$list = [
    ["post_id"=>"2","date"=>"2015","title"=>"title2"],
    ["post_id"=>"4","date"=>"2017","title"=>"title4"],
    ["post_id"=>"3","date"=>"2016","title"=>"title3"],
    ["post_id"=>"1","date"=>"2014","title"=>"title1"]
];

usort(
  $list,
  function($a, $b) {
    return $a['post_id'] < $b['post_id'];
  }
);
print_r($list); // First output, descending order

usort(
  $list,
  function($a, $b) {
    return $a['post_id'] > $b['post_id'];
  }
);
print_r($list); // Second output, ascending order

请注意两次调用中的反向比较运算符<> ...

显然的输出是:

第一个输出,降序:

Array
(
    [0] => Array
        (
            [post_id] => 4
            [date] => 2017
            [title] => title4
        )

    [1] => Array
        (
            [post_id] => 3
            [date] => 2016
            [title] => title3
        )

    [2] => Array
        (
            [post_id] => 2
            [date] => 2015
            [title] => title2
        )

    [3] => Array
        (
            [post_id] => 1
            [date] => 2014
            [title] => title1
        )

)

第二个输出,升序:

Array
(
    [0] => Array
        (
            [post_id] => 1
            [date] => 2014
            [title] => title1
        )

    [1] => Array
        (
            [post_id] => 2
            [date] => 2015
            [title] => title2
        )

    [2] => Array
        (
            [post_id] => 3
            [date] => 2016
            [title] => title3
        )

    [3] => Array
        (
            [post_id] => 4
            [date] => 2017
            [title] => title4
        )

)

答案 2 :(得分:1)

我使用这个功能:

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
    return $array;
}

$list = array (
    array("post_id"=>"2","date"=>"2015","title"=>"title2"),
    array("post_id"=>"4","date"=>"2017","title"=>"title4"),
    array("post_id"=>"3","date"=>"2016","title"=>"title3"),
    array("post_id"=>"1","date"=>"2014","title"=>"title1")
);

$outputArray = aasort($list,"post_id");

答案 3 :(得分:1)

这可能不是从这个问题中选择的最佳答案。只需提取要排序的列,然后对其进行排序以对原始内容进行排序:

array_multisort(array_column($list, 'post_id'), SORT_ASC, $list);

答案 4 :(得分:1)

您可以使用以下代码:

<?php
$list = array (
    array("post_id"=>"2","date"=>"2015","title"=>"title2"),
    array("post_id"=>"4","date"=>"2017","title"=>"title4"),
    array("post_id"=>"3","date"=>"2016","title"=>"title3"),
    array("post_id"=>"1","date"=>"2014","title"=>"title1")
);

$sorted = array_orderby($list, 'post_id', SORT_ASC);

echo "<pre>";
print_r($sorted);

function array_orderby()
{
    $args = func_get_args();
    $data = array_shift($args);
    foreach ($args as $n => $field) {
        if (is_string($field)) {
            $tmp = array();
            foreach ($data as $key => $row)
                $tmp[$key] = $row[$field];
            $args[$n] = $tmp;
            }
    }
    $args[] = &$data;
    call_user_func_array('array_multisort', $args);
    return array_pop($args);
}
?>

它将产生以下输出:

Array
(
    [0] => Array
        (
            [post_id] => 1
            [date] => 2014
            [title] => title1
        )

    [1] => Array
        (
            [post_id] => 2
            [date] => 2015
            [title] => title2
        )

    [2] => Array
        (
            [post_id] => 3
            [date] => 2016
            [title] => title3
        )

    [3] => Array
        (
            [post_id] => 4
            [date] => 2017
            [title] => title4
        )

)

答案 5 :(得分:1)

中的功能 OP问题确实:

  • 首先从子阵列($on)通过foreach收集所有值,然后通过original_array键$sortable_array将其绑定到$k。与$list[0]['post_id'];一样,收集于:$sortable_array[0];
  • 收集完所有值后,数组将使用switch对DESC或ASC(请参阅asort)进行排序,以保持index =&gt;值连接。所以$sortable_array看起来像是:[0=>2,1=>4,2=>3,3=>1] [3=>1,0=>2,2=>3,1=>4]之后
  • 所以现在对值进行排序,索引可以在下一步中使用。
  • 在最后foreach中生成一个新数组。来自$k的索引$sortable_array用于从新订单中获取original_array的子​​阵列。

注意:此部分if (is_array($v))使得函数行为无法预测,因为如果$v不是一个子阵列,它只需要$v来自子阵列的数据!