download1002 2010-12-16 04:15
浏览 7
已采纳

php - 匹配数组中的元素

I've been playing with this all day and haven't figured out a good way to do it...

I have two arrays and am trying to create an array based on matching values.

//$original

    Array
    (
        [0] => Array
            (
                [items] => Array
                    (
                        [0] => Array
                            (
                                [0] => PA
                                [1] => DZ
                                [2] => ER
                                [3] => TY
                            )

                        [1] => Array
                            (
                                [0] => KV
                                [1] => EN
                                [2] => CR
                            )

                        [2] => Array
                            (
                                [0] => HU
                                [1] => GO
                                [2] => GA
                                [3] => FI
                            )
                    )
            )
    )


//$compare    
    Array
    (
        [0] => Array
            (
                [items] => Array
                    (
                        [0] => Array
                            (
                                [0] => PA
                                [1] => AN
                                [2] => ER
                            )

                        [1] => Array
                            (
                                [0] => KV
                            )

                        [2] => Array
                            (
                                [0] => HU
                                [1] => XV
                                [2] => ZL
                                [3] => FI
                            )
                    )
            )
    )

And I'm trying to produce

//$similar
    Array
    (
        [0] => Array
            (
                [items] => Array
                    (
                        [0] => Array
                            (
                                [0] => PA
                                [2] => ER
                            )

                        [1] => Array
                            (
                                [0] => KV
                            )

                        [2] => Array
                            (
                                [0] => HU
                                [3] => FI
                            )
                    )
            )
    )
  • 写回答

1条回答 默认 最新

  • douao1959 2010-12-16 04:17
    关注

    Use array_intersect.

    $similar = $compare;
    $similar[0]['items'] = array_intersect($compare[0]['items'], $original[0]['items']);
    

    codepad example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?