php - 使用array_diff比较关联数组和数字数组

时间:2015-04-17 17:58:59

标签: php mysql arrays multidimensional-array

Array1是一个关联数组,包含从MySQL获取的多个列表,而Array2是一个数字数组,即一个简单的数组。 Array2有list_ids,用于过滤Array1。

Array1示例:

Array (
    [0] => Array (
        [id] => 1
        [list_id] => 68
        [list_value] => List-68
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
    [1] => Array (
        [id] => 2
        [list_id] => 69
        [list_value] => List-69
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
    [2] => Array (
        [id] => 3
        [list_id] => 80
        [list_value] => List-80
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
    [3] => Array (
        [id] => 4
        [list_id] => 81
        [list_value] => List-81
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
)

Array2:

[68, 81]

我的输出应该是:

Array (     
    [0] => Array (
        [id] => 2
        [list_id] => 69
        [list_value] => List-69
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
    [1] => Array (
        [id] => 3
        [list_id] => 80
        [list_value] => List-80
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
)

因此,在正常情况下array_diff()会这样做,但在这里它不会起作用。我尝试将数值Array2转换为关联并使用array_diff_assoc()函数,但仍然没有工作。

我还尝试迭代我的数值数组并检查关联Array1中的每个元素(即MySQL中每个获取的行),以查找数值数组中的每个数字。

foreach ($list as $val) {
    while ( $r = mysqli_fetch_assoc($result)) {
        echo $val."<br/>";
        if($r['list_id'] != $val ) {
                $new[] = $r;
        }
    }       
}
print_r($new);

但输出仅回显数字Array1的第一个数字4次(Array1的长度),因此只有一个list_id从关联Array2中移除而不是2:

68
68
68
68
Array (     
    [0] => Array (
        [id] => 2
        [list_id] => 69
        [list_value] => List-69
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
    [1] => Array (
        [id] => 3
        [list_id] => 80
        [list_value] => List-80
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )
    [2] => Array (
        [id] => 4
        [list_id] => 81
        [list_value] => List-81
        [subject] => "abc"
        [message] => "abc"
        [obj_var] => "abc"
        [url_link] => "abc"
        [list_time] => 2014-05-13 22:04:04
    )

)

那么,我怎样才能让它发挥作用?什么是最佳算法?

2 个答案:

答案 0 :(得分:1)

您可以使用foreach循环in_array检查unset不需要的元素:

foreach ($Array1 as $key => $value)
    if(in_array($valeu['list_id'], $Array2)) unset($list[$key]);

答案 1 :(得分:1)

我已将您的代码简化为以下内容。

$arr1 = array();
while ( $r = mysqli_fetch_assoc($result)) { $arr1[] = $r; }
$arr2 = array(68,81);

// the magic happends here. It will return an array with only the elements that matches the id of those values in $arr2
$filtered = array_filter($arr1, function ($val) use ($arr2) { return !in_array($val['id'], $arr2); });