0

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).

3
  • What should be the result in this example? array(3)? Commented Feb 17, 2011 at 4:28
  • do you want a simple comparision, or something more complex? Commented Feb 17, 2011 at 4:28
  • I want simple comparison Commented Feb 17, 2011 at 4:30

3 Answers 3

3
$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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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,
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.