这个问题我已经绞尽脑汁了。
我有两个数组:
数组 1 从 CSV 文件中提取。
数组 ( [0] => 数组 ( [uid] => cgajate [日期] => 20120918 ) [1] => 数组 ( [uid] => badrock5 [日期] => 20120920 ) [2] => 数组 ( [uid] => ricoetc [日期] => 20120921 ) [3] => 数组 ( [uid] => ricoetc1 [日期] => 20120923 ) [4] => 数组 ( [uid] => darbyfired [日期] => 20120922 ) [5] => 数组 ( [uid] => sagers.andrew [日期] => 20120922 ) [6] => 数组 ( [uid] => 法兰克福9 [日期] => 20120923 ) [7] => 数组 ( [uid] => 海滩男孩 [日期] => 20120923 ) [8] => 数组 ( [uid] => 潘特拉凡 [日期] => 20120923 ) [9] => 数组 ( [uid] => kingsxrules [日期] => 20120923 ) [10] => 数组 ( [uid] => 理查德.bitto [日期] => 20120924 ) [11] => 数组 ( [uid] => 克里斯托弗.boss [日期] => 20120925 ) [12] => 数组 ( [uid] => eric.robinson2 [日期] => 20120926 ) )
数组 2 从 SQL 数据库中提取。
数组 ( [0] => 数组 ( [uid] => cgajate [日期] => 20120919 ) [1] => 数组 ( [uid] => ricoetc [日期] => 20120921 ) [2] => 数组 ( [uid] => ricoetc1 [日期] => 20120922 ) [3] => 数组 ( [uid] => 法兰克福9 [日期] => 20120923 ) [4] => 数组 ( [uid] => 海滩男孩 [日期] => 20120923 ) [5] => 数组 ( [uid] => 潘特拉凡 [日期] => 20120923 ) [6] => 数组 ( [uid] => kingsxrules [日期] => 20120923 ) [7] => 数组 ( [uid] => eric.robinson2 [日期] => 20120926 ) )
我本质上要做的是检查是否有任何 UID 匹配。如果存在匹配项,它将检查哪个日期比另一个日期更新(因此本质上是哪个日期大于)。然后它会将它们添加到一个数组中,其中包含两个数组中 UID 不匹配的数据。
抱歉,如果很难理解。有点难以解释。
感谢任何和所有的帮助,提前谢谢大家。
由于 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;
虽然有点乱,但是很有效。
<?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>
假设您的示例数据准确地描述了您的 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,
)