Сравнить два массива

MilkeyWay

Постоялец
Регистрация
11 Июл 2011
Сообщения
142
Реакции
10
Всем привет,че-то не соображу,как вытащить из array1 product_id,если в массиве array2 нет совпадений с id,поделитесь мыслями

array1

Код:
Array
(
  [0] => Array
  (
  [product_id] => 407
  [price_retail] => 399
  [date_change] => 2016-10-24 18:20:43
  [video] => vun6t8
  )

  [1] => Array
  (
  [product_id] => 138
  [price_retail] => 149
  [date_change] => 2017-06-27 15:33:42
  [video] => 0yzJQQ
  )

  [2] => Array
  (
  [product_id] => 142
  [price_retail] => 399
  [date_change] => 2017-10-30 06:41:54
  [video] => 6qMabU
  )

  [3] => Array
  (
  [product_id] => 155
  [price_retail] => 199
  [date_change] => 2018-01-09 14:02:04
  [video] => tOTidcI
  )

  [4] => Array
  (
  [product_id] => 180
  [price_retail] => 119
  [date_change] => 2017-10-19 19:37:48
  [video] => dyBmiFM
  )

  [5] => Array
  (
  [product_id] => 199
  [price_retail] => 249
  [date_change] => 2017-06-27 15:40:16
  [video] => xB7PpvhTbfQ
  )
)

array2
Код:
Array
(
  [0] => Array
  (
  [id] => 407
  )

  [1] => Array
  (
  [id] => 5788
  )

  [2] => Array
  (
  [id] => 142
  )

  [3] => Array
  (
  [id] => 2088
  )
)
 
PHP:
$array1 = [
    [
        "product_id" => 407,
        "price_retail" => 399,
        "date_change" => "2016-10-24 18:20:43",
        "video" => "vun6t8",
    ], [
        "product_id" => 138,
        "price_retail" => 149,
        "date_change" => "2017-06-27 15:33:42",
        "video" => "0yzJQQ",
    ], [
        "product_id" => 142,
        "price_retail" => 399,
        "date_change" => "2017-10-30 06:41:54",
        "video" => "6qMabU",
    ], [
        "product_id" => 155,
        "price_retail" => 199,
        "date_change" => "2018-01-09 14:02:04",
        "video" => "tOTidcI",
    ], [
        "product_id" => 180,
        "price_retail" => 119,
        "date_change" => "2017-10-19 19:37:48",
        "video" => "dyBmiFM",
    ], [
        "product_id" => 199,
        "price_retail" => 249,
        "date_change" => "2017-06-27 15:40:16",
        "video" => "xB7PpvhTbfQ",
    ],
];

$array2 = [
    ["id" => 407],
    ["id" => 5788],
    ["id" => 142],
    ["id" => 2088],
];

$ids = array_column($array2, "id");

$result = array_filter($array1, function($a) use ($ids) {
    return !in_array($a["product_id"], $ids);
});

var_dump($result);
 
PHP:
$array1 = [
    [
        "product_id" => 407,
        "price_retail" => 399,
        "date_change" => "2016-10-24 18:20:43",
        "video" => "vun6t8",
    ], [
        "product_id" => 138,
        "price_retail" => 149,
        "date_change" => "2017-06-27 15:33:42",
        "video" => "0yzJQQ",
    ], [
        "product_id" => 142,
        "price_retail" => 399,
        "date_change" => "2017-10-30 06:41:54",
        "video" => "6qMabU",
    ], [
        "product_id" => 155,
        "price_retail" => 199,
        "date_change" => "2018-01-09 14:02:04",
        "video" => "tOTidcI",
    ], [
        "product_id" => 180,
        "price_retail" => 119,
        "date_change" => "2017-10-19 19:37:48",
        "video" => "dyBmiFM",
    ], [
        "product_id" => 199,
        "price_retail" => 249,
        "date_change" => "2017-06-27 15:40:16",
        "video" => "xB7PpvhTbfQ",
    ],
];

$array2 = [
    ["id" => 407],
    ["id" => 5788],
    ["id" => 142],
    ["id" => 2088],
];

$ids = array_column($array2, "id");

$result = array_filter($array1, function($a) use ($ids) {
    return !in_array($a["product_id"], $ids);
});

var_dump($result);
Благодарю,что не прошли мимо! Еще вот такой вариант подсказали:

Код:
if(array_search($item1['product_id'], array_column($array2, 'id')) !== false){continue;}
else{echo $item1['product_id'];}
 
Назад
Сверху