我如何比较数组值和子数组值

时间:2018-10-30 13:27:32

标签: php sub-array array-difference

我试图得到两个数组之间的差异 我有一个带有子数组的大数组和一个小的简单数组。 我想从大阵列中获得区别。

我用它来获取2个数组之间的差异,但是使用子数组则有些麻烦。 $ 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
        )
)

结果如下:

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

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

1 个答案:

答案 0 :(得分: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);

输出:

array (
    0 => 
    array (
    'id' => '1',
    ),
    1 => 
    array (
    'id' => '4',
    ),
)