简体   繁体   English

在具有ID的多维数组中获得最高日期

[英]getting highest date in multidimensional array with id

Where the sub arrays have similar id s, I want to select only those with the highest date. 在子数组具有相似id的地方,我只想选择日期最高的那些。

$array = [
           ['id' => 10, 'date' => '10-24-1994'],
           ['id' => 10, 'date' => '10-24-1996'],
           ['id' => 10, 'date' => '10-24-1997'],
           ['id' => 10, 'date' => '10-24-1998'],
           ['id' => 9, 'date' => '10-24-1998'],
           ['id' => 9, 'date' => '10-24-2001'],
           ['id' => 9, 'date' => '10-24-1997'],
           ['id' => 8, 'date' => '10-24-1996'],
           ['id' => 10, 'date' => '10-24-1999'],
           ['id' => 10, 'date' => '10-24-1991'],
           ['id' => 10, 'date' => '10-24-1993'],
           ['id' => 8, 'date' => '10-24-2001']
         ]; /* array i have */

$expected_Result = [
                     ['id' => 10, 'date' => '10-24-1999'],
                     ['id' => 9, 'date'=> '10-24-2001'],
                     ['id' => 8, 'date' => '10-24-2001'],
                   ];
 /* array which i expected */ 

I would use this as the solution, in order to have an array where the key is the id itself: 我将使用它作为解决方案,以便拥有一个数组,其中键是id本身:

/** 
 * @param array{id:int, date:string} $list
 *
 * @return array<int,string> 
 */
function sortDatesUniqueById(array $list): array
{
    $result = [];

    foreach ($list as ['id' => $id, 'date' => $date]) {
        if (!isset($result[$id]) || $result[$id] < $date) {
            $result[$id] = $date;
        }
    }

    return $result;
}

Or another approach, to get exactly what you are asking for: 或另一种方法,以准确获得您的要求:

/** 
 * @param array{id:int, date:string} $list
 *
 * @return array{id:int, date:string} 
 */
function sortDatesUniqueById(array $list): array
{
    $result = [];

    foreach ($list as ['id' => $id, 'date' => $date]) {
        if (!isset($result[$id]) || $result[$id]['date'] < $date) {
            $result[$id] = ['id' => $id, 'date' => $date];
        }
    }

    return array_values($result);
}

PHPDoc used based on the standard https://github.com/phan/phan (generic arrays and array shapes). 基于标准https://github.com/phan/phan (通用数组和数组形状)使用的PHPDoc。

Just loop over the array, check if that ID exists as an index in the $result array. 只需循环遍历数组,检查该ID是否作为$result数组中的索引存在。 If it does, check if the new date is bigger than the current one - if yes, overwrite it with the new value. 如果是这样,请检查新日期是否大于当前日期-如果是,则用新值覆盖它。

$result = [];
foreach ($array as $v) {
    if (!isset($result[$v['id']])) {
        $result[$v['id']] = $v['date'];
    } elseif ($result[$v['id']] < $v['date']) {
        $result[$v['id']] = $v['date'];
    }
}

Here's my solution. 这是我的解决方案。

$result = [];

foreach ($array as $value) {
    if (!in_array($value['id'], array_column($result, 'id'))) {
        $result[] = $value;
    } else {
        foreach ($result as $key => $res) {
            if ($res['id'] === $value['id'] && $value['date'] < $res['date']) {
                $result[$key]['date'] = $value['date'];
            }
        }
    }
}

First iterate over each value, if it is not in the list, add to the $result var, else, iterate over the $result value and check when the id is the same whith the current $value and finally check wich date is bigger. 在每个值首先迭代,如果它不在列表中,添加到$result VAR,否则,遍历$result值和检查时的ID是蒙山当前相同$value和最终检查至极日期是更大的。

You can user array_multisort() and finally array_walk() to make the array unique. 您可以使用array_multisort() ,最后使用array_walk()来使数组唯一。

array_multisort(
    array_column($array, 'id'), SORT_DESC, 
    array_column($array, 'date'), SORT_DESC, 
    $array
);

$result = [];
array_walk($array, function ($item) use (&$result) {
    !isset($result[$item['id']]) && $result[$item['id']] = $item['date'];
});

print '<pre>';
print_r($result);

Here is one liner, 这是一支班轮,
First sort by id desc and date asc. 首先按ID desc和日期asc排序。 Then make key as id so that last date of it will override every small dates. 然后将键设置为id,以便其最后日期将覆盖每个小日期。 Then to reset index use array_values . 然后使用array_values重置索引。

array_multisort(array_column($array, 'id'), SORT_DESC, array_column($array, 'date'), SORT_ASC, $array);
$result = array_values(array_column($array, null, "id"));
print_r($result);

array_column — Return the values from a single column in the input array array_column —从输入数组中的单个列返回值

Note: column_key: The column of values to return. 注意: column_key:要返回的值的列。 This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. 此值可以是您要检索的列的整数键,也可以是关联数组或属性名称的字符串键名。 It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array). 返回完整的数组或对象也可能为NULL (这与index_key一起用于重新索引数组很有用)。

Demo 演示版

Output:- 输出:-

Array
(
    [0] => Array
        (
            [id] => 10
            [date] => 10-24-1999
        )

    [1] => Array
        (
            [id] => 9
            [date] => 10-24-2001
        )

    [2] => Array
        (
            [id] => 8
            [date] => 10-24-2001
        )

)

We can sort the array first by id and date ascending, and then use array_column to pull out the highest dates. 我们可以先按ID和日期升序对数组进行排序,然后使用array_column提取最高日期。

$array = [
    ['id' => 10, 'date' => '10-24-1994'],
    ['id' => 10, 'date' => '10-24-1996'],
    ['id' => 10, 'date' => '10-24-1997'],
    ['id' => 10, 'date' => '10-24-1998'],
    ['id' => 9, 'date' => '10-24-1998'],
    ['id' => 9, 'date' => '10-24-2001'],
    ['id' => 9, 'date' => '10-24-1997'],
    ['id' => 8, 'date' => '10-24-1996'],
    ['id' => 10, 'date' => '10-24-1999'],
    ['id' => 10, 'date' => '10-24-1991'],
    ['id' => 10, 'date' => '10-24-1993'],
    ['id' => 8, 'date' => '10-24-2001']
]; 

uasort($array, function($a, $b) {
    $r = $a['id'] <=> $b['id'];
    if($r == 0)
        return $a['date']<=>$b['date'];
    return $r;
});

var_export(array_column($array, null, 'id'));

Output: 输出:

array (
  8 => 
  array (
    'id' => 8,
    'date' => '10-24-2001',
  ),
  9 => 
  array (
    'id' => 9,
    'date' => '10-24-2001',
  ),
  10 => 
  array (
    'id' => 10,
    'date' => '10-24-1999',
  ),
)

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

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