I've been racking my brain with this one.
I have two arrays:
Array 1 pulls from a CSV file.
Array ( [0] => Array ( [uid] => cgajate [date] => 20120918 ) [1] => Array ( [uid] => badrock5 [date] => 20120920 ) [2] => Array ( [uid] => ricoetc [date] => 20120921 ) [3] => Array ( [uid] => ricoetc1 [date] => 20120923 ) [4] => Array ( [uid] => darbyfired [date] => 20120922 ) [5] => Array ( [uid] => sagers.andrew [date] => 20120922 ) [6] => Array ( [uid] => frankfurt9 [date] => 20120923 ) [7] => Array ( [uid] => beachboys [date] => 20120923 ) [8] => Array ( [uid] => panterafan [date] => 20120923 ) [9] => Array ( [uid] => kingsxrules [date] => 20120923 ) [10] => Array ( [uid] => richard.bitto [date] => 20120924 ) [11] => Array ( [uid] => christopher.boss [date] => 20120925 ) [12] => Array ( [uid] => eric.robinson2 [date] => 20120926 ) )
Array 2 pulls from the SQL database.
Array ( [0] => Array ( [uid] => cgajate [date] => 20120919 ) [1] => Array ( [uid] => ricoetc [date] => 20120921 ) [2] => Array ( [uid] => ricoetc1 [date] => 20120922 ) [3] => Array ( [uid] => frankfurt9 [date] => 20120923 ) [4] => Array ( [uid] => beachboys [date] => 20120923 ) [5] => Array ( [uid] => panterafan [date] => 20120923 ) [6] => Array ( [uid] => kingsxrules [date] => 20120923 ) [7] => Array ( [uid] => eric.robinson2 [date] => 20120926 ) )
What I essentially what to do is check if there are any UID matches. If there are matches, it will check which date is more current than the other (so essentially what date is greater than). Then it will add them to an array with the data that UIDs didn't match from both arrays.
Sorry if it's hard to understand. It's slightly hard to explain.
Any and all help is appreciated, thank you all in advance.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With