Christopher
Christopher

Reputation: 91

Merge and flatten two 2d arrays to create an associative array with ids as keys and the latest date per id as values

I have two arrays:

Array 1 pulls from a CSV file.

[
    ["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]
]

Array 2 pulls from the SQL database.

[
    ["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]
]

What I essentially want to do is build a flat associative array of unique uids and preserve the greater date when a uid occurs more than once.

Desired output:

[
  '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
]

Upvotes: 6

Views: 4173

Answers (3)

mickmackusa
mickmackusa

Reputation: 48070

Assuming your sample data accurately depicts that your CSV-borne array will have no duplicated uid values, you can swiftly set the result array with thos array's data as an associative array using array_column().

Then traverse the SQL-borne array and make conditional associative declarations when a new uid or later date are encountered. Demo

$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);

Output:

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,
)

Upvotes: 0

Peter Sobot
Peter Sobot

Reputation: 2557

As PHP arrays are themselves hash maps, you could iterate through one array and insert each date into a new array, keyed by UID:

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

Then, you could iterate through the second array, checking if any of the UIDs already exist as keys in the $out array. If the UID already exists, then you can compare dates and take whichever piece of data you prefer. For example, something like:

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

Then, to flatten the data back down:

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

Upvotes: 3

David Grenier
David Grenier

Reputation: 1241

It's a bit messy but it works.

<?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>

Upvotes: 0

Related Questions