合并并展平两个二维数组以创建一个关联数组,其中 ids 作为键,每个 id 的最新日期作为值

问题描述 投票:0回答:3

我有两个数组:

数组 1 从 CSV 文件中提取。

[
    ["uid" => "cgajate", "date" => 20120918],
    ["uid" => "badrock5", "date" => 20120920],
    ["uid" => "ricoetc", "date" => 20120921],
    ["uid" => "ricoetc1", "date" => 20120923],
    ["uid" => "darbyfired", "date" => 20120922],
    ["uid" => "sagers.andrew", "date" => 20120922],
    ["uid" => "frankfurt9", "date" => 20120923],
    ["uid" => "beachboys", "date" => 20120923],
    ["uid" => "panterafan", "date" => 20120923],
    ["uid" => "kingsxrules", "date" => 20120923],
    ["uid" => "richard.bitto", "date" => 20120924],
    ["uid" => "christopher.boss", "date" => 20120925],
    ["uid" => "eric.robinson2", "date" => 20120926]
]

数组 2 从 SQL 数据库中提取。

[
    ["uid" => "cgajate", "date" => 20120919],
    ["uid" => "ricoetc", "date" => 20120921],
    ["uid" => "ricoetc1", "date" => 20120922],
    ["uid" => "frankfurt9", "date" => 20120923],
    ["uid" => "beachboys", "date" => 20120923],
    ["uid" => "panterafan", "date" => 20120923],
    ["uid" => "kingsxrules", "date" => 20120923],
    ["uid" => "eric.robinson2", "date" => 20120926]
]

我本质上想做的是构建一个唯一 uid 的平面关联数组,并在 uid 多次出现时保留更大的日期。

所需输出:

[
  'cgajate' => 20120919,
  'badrock5' => 20120920,
  'ricoetc' => 20120921,
  'ricoetc1' => 20120923,
  'darbyfired' => 20120922,
  'sagers.andrew' => 20120922,
  'frankfurt9' => 20120923,
  'beachboys' => 20120923,
  'panterafan' => 20120923,
  'kingsxrules' => 20120923,
  'richard.bitto' => 20120924,
  'christopher.boss' => 20120925,
  'eric.robinson2' => 20120926
]
php arrays multidimensional-array grouping associative-array
3个回答
3
投票

由于 PHP 数组本身就是哈希映射,因此您可以迭代一个数组并将每个日期插入到一个新数组中,并以 UID 为键:

$out = array();
foreach ($first_array as $x) {
    $out[$x['uid']] = $x['date'];
}

然后,您可以迭代第二个数组,检查是否有任何 UID 已作为键存在于

$out
数组中。如果 UID 已经存在,那么您可以比较日期并获取您喜欢的任何数据。例如,类似:

foreach ($second_array as $y) {
    if (array_key_exists($y['uid'], $out)) {
        if ($out[$y['uid']] < $y['date']) {
            $out[$y['uid']] = $y['date'];
        }
    } else {
        $out[$y['uid']] = $date;
    }
}

然后,将数据扁平化:

$_out = array();
foreach ($out as $uid => $date) {
    $_out[] = array("uid" => $uid, "date" => $date);
}
$out = $_out;

0
投票

虽然有点乱,但是很有效。

<?php

$arr1 = array(
    array("uid" => "cgajate", "date" => 20120918),
    array("uid" => "badrock5", "date" => 20120920),
    array("uid" => "ricoetc", "date" => 20120921),
    array("uid" => "ricoetc1", "date" => 20120923),
    array("uid" => "darbyfired", "date" => 20120922),
    array("uid" => "sagers.andrew", "date" => 20120922),
    array("uid" => "frankfurt9", "date" => 20120923),
    array("uid" => "beachboys", "date" => 20120923),
    array("uid" => "panterafan", "date" => 20120923),
    array("uid" => "kingsxrules", "date" => 20120923),
    array("uid" => "richard.bitto", "date" => 20120924),
    array("uid" => "christopher.boss", "date" => 20120925),
    array("uid" => "eric.robinson2", "date" => 20120926));


$arr2 = Array(
    array("uid" => "cgajate", "date" => 20120919),
    array("uid" => "ricoetc", "date" => 20120921),
    array("uid" => "ricoetc1", "date" => 20120922),
    array("uid" => "frankfurt9", "date" => 20120923),
    array("uid" => "beachboys", "date" => 20120923),
    array("uid" => "panterafan", "date" => 20120923),
    array("uid" => "kingsxrules", "date" => 20120923),
    array("uid" => "eric.robinson2", "date" => 20120926));

function flatten ($arr) {
    $new_arr = array ();
    foreach ($arr as $sub_arr) {
        $new_arr[$sub_arr["uid"]] = $sub_arr["date"];
    } 
    return $new_arr;
}


$flat_arr1 = flatten ($arr1);
$flat_arr2 = flatten ($arr2);
$arr3 = array ();

foreach ($flat_arr1 as $key=>$value) {
    if (isset ($flat_arr2[$key])) {
        $value = $flat_arr1[$key] > $flat_arr2[$key] ? $flat_arr1[$key] : $flat_arr2[$key];
    }
    $arr3[$key] = $value;
}

foreach ($flat_arr2 as $key=>$value) {
    if (isset ($flat_arr1[$key])) {
        $value = $flat_arr1[$key] > $flat_arr2[$key] ? $flat_arr1[$key] : $flat_arr2[$key];
    }
    $arr3[$key] = $value;
}

?> 

<pre><?php print_r($flat_arr1); ?></pre>

<pre><?php print_r($flat_arr2); ?></pre>

<pre><?php print_r($arr3); ?></pre>

0
投票

假设您的示例数据准确地描述了您的 CSV 数组不会有重复的 uid 值,您可以使用

array_column()
快速将结果数组与该数组的数据设置为关联数组。

然后遍历 SQL 承载的数组,并在遇到新的 uid 或更新的日期时进行条件关联声明。 演示

$result = array_column($csv, 'date', 'uid');
foreach ($sql as ['uid' => $id, 'date' => $date]) {
    if ($date > ($result[$id] ?? PHP_INT_MIN)) {
        $result[$id] = $date;
    }
}
var_export($result);

输出:

array (
  'cgajate' => 20120919,
  'badrock5' => 20120920,
  'ricoetc' => 20120921,
  'ricoetc1' => 20120923,
  'darbyfired' => 20120922,
  'sagers.andrew' => 20120922,
  'frankfurt9' => 20120923,
  'beachboys' => 20120923,
  'panterafan' => 20120923,
  'kingsxrules' => 20120923,
  'richard.bitto' => 20120924,
  'christopher.boss' => 20120925,
  'eric.robinson2' => 20120926,
)
© www.soinside.com 2019 - 2024. All rights reserved.