【问题标题】:Sorting two associative arrays based on different keys根据不同的键对两个关联数组进行排序
【发布时间】:2014-06-29 14:28:33
【问题描述】:

我有以下两个关联数组:

$arr1 = array(
    'id' => 1,
    'text' => 'Some text is here',
    'timestamp' => 130458750
)

$arr2 = array(
    'post_id' => 12,
    'content' => 'Some content is here too',
    'created_at' => 1402154823
)

我想根据 timestampcreated_at 键对这两个数组进行排序,即较大的整数是第一个,较小的第二个,依此类推。这可能使用PHP的内置函数吗?如果不是,我该如何解决这个问题?

编辑 期望的结果是: 这里,$arr1 的时间戳较小,$arr2 的时间戳(即 created_at)较大。所以,我想得到$arr1$arr2 的组合,其中$arr2 是第一,$arr1 是第二。比如:

$sorted_arr = array($arr2, $arr1);

【问题讨论】:

  • 数组中只有一个时间戳,你在排序什么?
  • 这两个数组实际上是二维数组的元素吗,你要排序吗?
  • @Barmar,其实有两个时间戳,一个是timestamp,另一个是created_at。我想根据这两个值对这两个数组进行排序。
  • @FerozAkbar,是的,它们是关联的。
  • 请显示期望的结果。

标签: php arrays sorting data-structures


【解决方案1】:

首先让我说你的一个数组包含timestamp,第二个包含created_at。我假设他们都应该是created_at

如果您只想“排序”两个条目,就像您在 cmets 中所说的那样,任务很简单:

<?php
$arr1 = array(
    'id' => 1,
    'text' => 'Some text is here',
    'created_at' => 130458750 #corrected from "timestamp"
    );

$arr2 = array(
    'post_id' => 12,
    'content' => 'Some content is here too',
    'created_at' => 1402154823
    );

$posts = $arr2['created_at'] > $arr1['created_at']
    ? [$arr2, $arr1]
    : [$arr1, $arr2];

但显然,如果帖子位于未知长度的数组中,您所追求的是一种对帖子进行排序的方法。在这种情况下,您应该使用uasort 内置 PHP 函数,它允许按用户定义的函数排序并在关联数组中维护索引(与普通的 usort 相对)。示例代码将如下所示:

$posts = [$arr1, $arr2];

uasort($posts, function($a, $b)
{
    return $b['created_at'] - $a['created_at'];
});

var_dump($posts);

哪个输出:

array(2) {
  [1]=>
  array(3) {
    ["post_id"]=>
    int(12)
    ["content"]=>
    string(24) "Some content is here too"
    ["created_at"]=>
    int(1402154823)
  }
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["text"]=>
    string(17) "Some text is here"
    ["created_at"]=>
    int(130458750)
  }
}

要获得相反的顺序,您可以在自定义排序函数中反转参数,即将$a$b 交换。

【讨论】:

  • 问题是第一个数组created_at键不存在。我需要在比较函数中使用 array_key_exists 吗?
  • 是的,或者更好的是,您可以确保您的数组始终包含相同的键。
  • 谢谢!我无法确定密钥是否相同,因为数据是从第三方网站获取的。
  • 感谢您的帮助!我写了这个函数。请检查我的答案。如果需要改进,请告诉我。
【解决方案2】:

结合rr-的solution,我想出了以下:

$arr1 = array(
    'id' => 1,
    'text' => 'Some text is here',
    'timestamp' => 130458750
);

$arr2 = array(
    'post_id' => 12,
    'content' => 'Some content is here too',
    'created_at' => 1402154823
);

$arr3 = array(
    'post_id' => 21,
    'content' => 'Some content is here too',
    'created_at' => 1258475
);
$arr = [];
$arr[] = $arr1;
$arr[] = $arr2;
$arr[] = $arr3;
uasort($arr, function($a, $b)
{
    $t1 = isset($a['timestamp']) ? $a['timestamp'] : $a['created_at'];
    $t2 = isset($b['timestamp']) ? $b['timestamp'] : $b['created_at']; 
    return $t2 - $t1
});
var_dump($arr);

即使键不同,它也会对数组进行排序。

【讨论】:

  • 您总是可以使用$t1 = isset($a['timestamp']) ? $a['timestamp'] : $a['created_at']; $t2 = isset($b['timestamp']) ? $b['timestamp'] : $b['created_at']; return $t2 - $t1;,或者更好的是,制作单独的函数function extractTimestamp($array),您可以在其中处理各种输入。 (也许将其设为匿名并将其存储在变量中以避免污染全局函数命名空间。)
猜你喜欢
  • 2019-08-10
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多