使用平面黑名单数组过滤二维数组的每一行

发布于 2024-10-17 22:55:15 字数 356 浏览 6 评论 0原文

我有一个带有黑名单值的平面数组,

Array ( [0] => 1 [1] => 2 )

我需要使用该黑名单从以下二维数组中删除元素。

Array (
    [0] => Array ( [id_1] => 3 [id_2] => 1 )
    [1] => Array ( [id_3] => 5 [id_4] => 3 )
) 

我想比较这些数组并获取一个包含二维关联数组中不存在的值的数组。

结果应为 array(3)array(5,3)

I have a flat array with blacklisted values

Array ( [0] => 1 [1] => 2 )

I need to use that blacklist to remove elements from the following 2d array.

Array (
    [0] => Array ( [id_1] => 3 [id_2] => 1 )
    [1] => Array ( [id_3] => 5 [id_4] => 3 )
) 

I want to compare these arrays and get an array containing the values that are not present in the 2d associative array.

The result should be array(3) and array(5,3).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余厌 2024-10-24 22:55:15
$array1 = array(1,2);

$array2 = array(
    array(
        'id_1' => 3,
        'id_2' => 1
    ),
    array(
        'id_3' => 5,
        'id_4' => 3,
    )
);

我不确定我是否理解这个问题,如果您想单独比较每个数组:

foreach($array2 as $subarray) {
    var_dump(array_diff($subarray, $array1));
}

return:

array
  'id_1' => int 3
array
  'id_3' => int 5
  'id_4' => int 3

否则,如果您不想循环并展平数组(使用 array_values 的 php 文档注释中的 array_flatten 函数)

< a href="http://www.php.net/manual/en/function.array-values.php#97715" rel="nofollow">http://www.php.net/manual/en/function。 array-values.php#97715

function array_flatten($a,$f=array()){
    if(!$a||!is_array($a))return '';
    foreach($a as $k=>$v){
        if(is_array($v))$f=array_flatten($v,$f);
        else $f[$k]=$v;
    }
    return $f;
}


var_dump(
    array_unique(
        array_diff(
            array_flatten($array2), $array1
        )
    )
);

返回:

array
  'id_1' => int 3
  'id_3' => int 5

“id_4”未显示,因为它没有唯一值。您可以使用 array_values() 删除键,或者通过从代码中删除 array_unique() 保留“id_4”。

$array1 = array(1,2);

$array2 = array(
    array(
        'id_1' => 3,
        'id_2' => 1
    ),
    array(
        'id_3' => 5,
        'id_4' => 3,
    )
);

I'm not sure if I understand the question, if you want to compare each array individually:

foreach($array2 as $subarray) {
    var_dump(array_diff($subarray, $array1));
}

returns:

array
  'id_1' => int 3
array
  'id_3' => int 5
  'id_4' => int 3

Otherwise if you don't want to loop through and flatten the array (using an array_flatten function from the php doc comments of array_values)

http://www.php.net/manual/en/function.array-values.php#97715

function array_flatten($a,$f=array()){
    if(!$a||!is_array($a))return '';
    foreach($a as $k=>$v){
        if(is_array($v))$f=array_flatten($v,$f);
        else $f[$k]=$v;
    }
    return $f;
}


var_dump(
    array_unique(
        array_diff(
            array_flatten($array2), $array1
        )
    )
);

returns:

array
  'id_1' => int 3
  'id_3' => int 5

'id_4' is not shown because it doesn't have a unique value. You can remove the keys with array_values() or leave in 'id_4' by removing the array_unique() from the code.

拔了角的鹿 2024-10-24 22:55:15

也许是这样的?

$array_a = array(1, 2);
$array_b = array(array(1, 3), array(3, 1));

$result = array();

foreach($array_b as $key => $sub_array_b){
    foreach($sub_array_b as $sub_key => $value){
        if(!in_array($value, $array_a) && !in_array($value, $result)) array_push($result, $value);        
    }
}

编辑:我在您编辑问题之前输入了此内容。您仍然可以修改上面的代码,以便为关联数组中的每个子数组创建一个不同的数组。

something like this, maybe?

$array_a = array(1, 2);
$array_b = array(array(1, 3), array(3, 1));

$result = array();

foreach($array_b as $key => $sub_array_b){
    foreach($sub_array_b as $sub_key => $value){
        if(!in_array($value, $array_a) && !in_array($value, $result)) array_push($result, $value);        
    }
}

EDIT: I typed this before you edited your question. You can still modify the code above so that it creates a different array for each subarray in your associative array.

属性 2024-10-24 22:55:15

对于过滤行数据的函数式方法,请使用 array_map() 迭代调用 array_diff()

$array1 = [1, 2];

$array2 = [
    ['id_1' => 3, 'id_2' => 1],
    ['id_3' => 5, 'id_4' => 3],
];

var_export(
    array_map(fn($row) => array_diff($row, $array1), $array2)
);

输出:

array (
  0 => 
  array (
    'id_1' => 3,
  ),
  1 => 
  array (
    'id_3' => 5,
    'id_4' => 3,
  ),
)

For a functional-style approach to filtering row data, use array_map() to make iterated calls of array_diff().

$array1 = [1, 2];

$array2 = [
    ['id_1' => 3, 'id_2' => 1],
    ['id_3' => 5, 'id_4' => 3],
];

var_export(
    array_map(fn($row) => array_diff($row, $array1), $array2)
);

Output:

array (
  0 => 
  array (
    'id_1' => 3,
  ),
  1 => 
  array (
    'id_3' => 5,
    'id_4' => 3,
  ),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文