数组差异

时间:2011-07-22 17:11:50

标签: php cakephp cakephp-1.3

我有这个数组让我们称之为数组1

Array
(
    [0] => Array
        (
            [Machine] => Array
                (
                    [id] => 7
                    [name] => XYZ
                    [priority] => 1
                )

            [Software] => Array
                (
                    [id] => 472
                )

        )

    [1] => Array
        (
            [Machine] => Array
                (
                    [id] => 6
                    [name] => ABC
                    [priority] => 0
                )

            [Software] => Array
                (
                    [id] => 470
                )

        )

    [2] => Array
        (
            [Machine] => Array
                (
                    [id] => 1
                    [name] => IEU
                    [priority] => 3
                )

            [Software] => Array
                (
                    [id] => 471
                )

        )

)

然后我有另一个数组让我们称之为数组2

Array
(
    [0] => 7
    [1] => 5
    [2] => 4
    [3] => 3
    [4] => 6
)

如果数组2没有[机器] [id],那么我希望它从数组1中删除。如上例1中将删除

   [2] => Array
            (
                [Machine] => Array
                    (
                        [id] => 1
                        [name] => IEU
                        [priority] => 3
                    )

                [Software] => Array
                    (
                        [id] => 471
                    )

            )

关于如何实现这一目标的任何想法。感谢

2 个答案:

答案 0 :(得分:3)

也许..

foreach ($array1 AS $key => $array) {
    if (!in_array($array['Machine']['id'], $array2))
       unset($array1[$key]);
}

答案 1 :(得分:1)

尝试类似:

$new_array = array();
foreach ($array1 as $platform)
{
     if (in_array($platform["Machine"]["id"], $array2))
     {
         $new_array[] = $platform;
     }
}

return $new_array;