Welcome Guest, Not a member yet? Register   Sign In
Array Question: return subset of items not shared by two arrays
#1

[eluser]gscharlemann[/eluser]
Hi

I have two arrays:
Code:
$in_list = Array
(
    [0] => Array
        (
            [item_id] => 8
        )

    [1] => Array
        (
            [item_id] => 16
        )

    [2] => Array
        (
            [item_id] => 17
        )
)

$full_list = Array
(
    [0] => Array
        (
            [id] => 8
            [name] => General
            [size] => 15
            [type] => feet
        )

    [1] => Array
        (
            [id] => 9
            [name] => Specific
            [size] => 1
            [type] => inches
        )
...
)

I would like to return an array that is the full_list minus the items in_list when comparing:
Code:
$full_list['id'] == $in_list['item_id']
I came up with the following(which works), but thought there might be a better way?
Code:
$items_to_remove = array();
        $iii = 0;
        for($i = 0; $i < count($full_list); $i++)
        {
            $item = $full_list[$i];
            for($ii = 0; $ii < count($in_list); $ii++)
            {
                $item_in_list = $in_list[$ii];
                if($item['id'] == $item_in_list['item_id'])
                {
                    $items_to_remove[$iii] = $i;
                    $iii++;
                    break;
                }
            }
        }
        for($i = 0; $i < count($items_to_remove); $i++)
        {
            unset($full_list[$items_to_remove[$i]]);
        }
Any thoughts? Thanks
#2

[eluser]richzilla[/eluser]
php's native array_intersect function.
http://php.net/manual/en/function.array-intersect.php




Theme © iAndrew 2016 - Forum software by © MyBB