数组与PHP中的不同维度相交

时间:2016-08-04 11:45:32

标签: array-intersect

我需要获得具有不同尺寸的2个阵列的交集,但我不知道array_intersect()是否可以使用它。

我试着这样做:

$result=array_intersect ($ONE, $TWO)

但它不起作用。

$ ONE:

Array
(
    [0] => Array
        (
            [name] => monday-1
            [title] => monday-1
            [id] => 2878
        )

    [1] => Array
        (
            [name] => tuesday-1
            [title] => tuesday-1
            [id] => 3180
        )

    [2] => Array
        (
            [name] => friday-1
            [title] => friday-1
            [id] => 3181
        )

)

和$ TWO:

    Array
(
    [1] => monday-1
    [2] => tuesday-1

)

所以我需要得到

Array
(
    [0] => Array
        (
            [name] => monday-1
            [title] => monday-1
            [id] => 2878
        )

    [1] => Array
        (
            [name] => tuesday-1
            [title] => tuesday-1
            [id] => 3180
        )
)

1 个答案:

答案 0 :(得分:0)

我想我有一个解决方案:

 for ($i = 1; $i <= count($two); $i++)
{
    for ($j = 0; $j < count($one) ; $j++){
          if ($two[$i] == $one[$j]['name']  )
        {
           $result[$j]['name']=$one[$j]['name'];
           $result[$j]['title']=$one[$j]['title'];
           $result[$j]['id']=$one[$j]['id'];

        }
    }
    }