-2

I try to get the differences between two arrays I have one big array with subarray and a small simple array. I want to get the difference from the big array.

I use this to get the differences between 2 arrays, but working with subarray is someting else. $array3 = array_diff($bigArray, $smallArray);

$smallArray = Array
(
    [0] => 2  (how i compare this values)
    [1] => 3  (how i compare this values)
)

$bigArray = Array
(
    [0] => Array
        (
            [g_id] => 2  (with this value)
            [g_nume] => Arad I Betel
        )

    [1] => Array
        (
            [g_id] => 3  (with this value)
            [g_nume] => Arad IV Agape
        )

    [2] => Array
        (
            [g_id] => 4  (with this value)
            [g_nume] => Frumuseni
        )

    [3] => Array
        (
            [g_id] => 7  (with this value)
            [g_nume] => Cuvin
        )
)

And the result to be like this:

Array
    (
        [0] => Array
            (
                [g_id] => 4  (with this value)
                [g_nume] => Frumuseni
            )

        [1] => Array
            (
                [g_id] => 7  (with this value)
                [g_nume] => Cuvin
            )
    )
oxido
  • 1
  • 4
  • And the code you have written to try and do this for yourself is .......? – RiggsFolly Oct 30 '18 at 13:29
  • Welcome, to improve your experience on SO please read how to ask an [On Topic question](https://stackoverflow.com/help/on-topic), and the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [TAKE THE TOUR](http://stackoverflow.com/tour) **We are very willing to help you fix your code, but we dont write it for you** – RiggsFolly Oct 30 '18 at 13:29
  • I do not know how to deal with the situation, because if I knew I did not ask you anymore. – oxido Oct 30 '18 at 14:01

1 Answers1

0
<?php

$disard_ids = [
    '2', '3'
];

$items = 
[
    [
        'id'=>'1'
    ],
    [
        'id'=>'2'
    ],
    [
        'id'=>'3'
    ],
    [
        'id'=>'4'
    ]
];

foreach ($items as $item) {
    if(array_search($item['id'], $discard_ids) === false) {
        $result[] = $item;
    }
}

var_export($result);

Output:

array (
    0 => 
    array (
    'id' => '1',
    ),
    1 => 
    array (
    'id' => '4',
    ),
)
Progrock
  • 7,373
  • 1
  • 19
  • 25