我一直用这个来绞尽脑汁。
我有两个数组:
数组1从CSV文件中提取。
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 ) )
数组2从SQL数据库中提取。
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 ) )
我基本上要做的是检查是否有任何UID匹配。如果有匹配,它将检查哪个日期比另一个更新(所以基本上什么日期大于)。然后它会将它们添加到一个数组中,其中UID与两个数组都不匹配。
很抱歉,如果这很难理解。这有点难以解释。
感谢所有的帮助,谢谢大家。
答案 0 :(得分: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;
答案 1 :(得分: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>