6

我一直在用这个来绞尽脑汁。

我有两个数组:

数组 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] => frankfurt9
            [日期] => 20120923
        )

    [7] => 数组
        (
            [uid] => 沙滩男孩
            [日期] => 20120923
        )

    [8] => 数组
        (
            [uid] => 惹火
            [日期] => 20120923
        )

    [9] => 数组
        (
            [uid] => kingsxrules
            [日期] => 20120923
        )

    [10] => 数组
        (
            [uid] => 理查德.bitto
            [日期] => 20120924
        )

    [11] => 数组
        (
            [uid] => christopher.boss
            [日期] => 20120925
        )

    [12] => 数组
        (
            [uid] => eric.robinson2
            [日期] => 20120926
        )

)

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

大批
(
    [0] => 数组
        (
            [uid] => cgajate
            [日期] => 20120919
        )

    [1] => 数组
        (
            [uid] => ricoetc
            [日期] => 20120921
        )

    [2] => 数组
        (
            [uid] => ricoetc1
            [日期] => 20120922
        )

    [3] => 数组
        (
            [uid] => frankfurt9
            [日期] => 20120923
        )

    [4] => 数组
        (
            [uid] => 沙滩男孩
            [日期] => 20120923
        )

    [5] => 数组
        (
            [uid] => 惹火
            [日期] => 20120923
        )

    [6] => 数组
        (
            [uid] => kingsxrules
            [日期] => 20120923
        )

    [7] => 数组
        (
            [uid] => eric.robinson2
            [日期] => 20120926
        )

)

我基本上要做的是检查是否有任何 UID 匹配。如果有匹配项,它将检查哪个日期比另一个日期更新(所以本质上是哪个日期大于)。然后它将它们添加到一个数组中,其中 UID 与两个数组中的数据都不匹配。

对不起,如果很难理解。有点难以解释。

任何和所有的帮助表示赞赏,在此先感谢大家。

4

2 回答 2

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;
于 2012-09-29T03:21:52.090 回答
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>
于 2012-09-29T05:31:26.523 回答