G
G
GRO242022-03-27 21:13:53
PHP
GRO24, 2022-03-27 21:13:53

Deleting in an array by the value of another array?

Hello.
Tell me how to quickly delete an array in a multidimensional array by value from another array.
For example:
The first array from which we will delete

array(3) {
  [0]=>
  array(9) {
    ["id"]=>
    string(1) "1"
    ["colmn1"]=>
    string(9) "TEST name"
    ["ticker"]=>
    string(4) "TEST"
    ["value"]=>
    string(3) "100"
    ["uniq_id"]=>
    string(32) "51cac2ed2fee4ec3a074a9e04f89887b"
  }
  [1]=>
  array(9) {
    ["id"]=>
    string(1) "2"
    ["colmn1"]=>
    string(9) "TEST2"
    ["ticker"]=>
    string(4) "TEST2"
    ["value"]=>
    string(3) "100"
    ["uniq_id"]=>
    string(32) "902b29f87418415893372792d6913e31"
  }
  [2]=>
  array(9) {
    ["id"]=>
    string(1) "3"
    ["colmn1"]=>
    string(9) "TEST3"
    ["ticker"]=>
    string(4) "TEST3"
    ["value"]=>
    string(3) "100"
    ["uniq_id"]=>
    string(32) "76246b76f0f0423b8b7f75634afab64e"
  }
}


The second array contains what needs to be removed.
array(2) {
  [0]=>
  string(22) "76246b76f0f0423b8b7f75634afab64e"
  [1]=>
  string(15) "902b29f87418415893372792d6913e31"
}


Search by the uniq_id field of the first array
If there is such an id, I need to delete the array containing this field

Everyone who will respond - thanks and have a good evening)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2022-03-28
@GRO24

It is natural to use array_filter to filter an array :

$res = array_filter(
  $arr,
  fn($el)=>!in_array($el["uniq_id"], $filter)
);

D
Dmitry Gordinskiy, 2022-03-27
@DmitriyGordinskiy

unset

Well, exactly what not to do:

array_diff_key(array_column($array, null, 'uniq_id'), array_flip($excludedKeys));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question