仅比较和返回数组中的匹配项

时间:2012-08-14 15:51:34

标签: php arrays

我正在寻找一种方法(在PHP中)使用两个不同的数组来获得匹配项的结果。阵列是运动属性。一个数组包含统计数据的定义,另一个数组包含stat_id和玩家的stat值。每个数组中的匹配项是stat_id。我希望得到两个数组中只匹配项目的结果。

第一个数组包含统计定义。这是stats定义数组的一部分...

[stat] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [stat_id] => 4
                [enabled] => 1
                [name] => Passing Yards
                [display_name] => Pass Yds
                [sort_order] => 1
                [position_type] => O
            )

        [1] => SimpleXMLElement Object
            (
                [stat_id] => 5
                [enabled] => 1
                [name] => Passing Touchdowns
                [display_name] => Pass TD
                [sort_order] => 1
                [position_type] => O
            )

        [2] => SimpleXMLElement Object
            (
                [stat_id] => 6
                [enabled] => 1
                [name] => Interceptions
                [display_name] => Int
                [sort_order] => 0
                [position_type] => O
            )

        [3] => SimpleXMLElement Object
            (
                [stat_id] => 9
                [enabled] => 1
                [name] => Rushing Yards
                [display_name] => Rush Yds
                [sort_order] => 1
                [position_type] => O
            )

        [4] => SimpleXMLElement Object
            (
                [stat_id] => 10
                [enabled] => 1
                [name] => Rushing Touchdowns
                [display_name] => Rush TD
                [sort_order] => 1
                [position_type] => O
            )

        [5] => SimpleXMLElement Object
            (
                [stat_id] => 12
                [enabled] => 1
                [name] => Reception Yards
                [display_name] => Rec Yds
                [sort_order] => 1
                [position_type] => O
            )

        [6] => SimpleXMLElement Object
            (
                [stat_id] => 13
                [enabled] => 1
                [name] => Reception Touchdowns
                [display_name] => Rec TD
                [sort_order] => 1
                [position_type] => O
            )

        [7] => SimpleXMLElement Object
            (
                [stat_id] => 15
                [enabled] => 1
                [name] => Return Touchdowns
                [display_name] => Ret TD
                [sort_order] => 1
                [position_type] => O
            )

        [8] => SimpleXMLElement Object
            (
                [stat_id] => 16
                [enabled] => 1
                [name] => 2-Point Conversions
                [display_name] => 2-PT
                [sort_order] => 1
                [position_type] => O
            )

        [9] => SimpleXMLElement Object
            (
                [stat_id] => 18
                [enabled] => 1
                [name] => Fumbles Lost
                [display_name] => Fum Lost
                [sort_order] => 0
                [position_type] => O
            )

        [10] => SimpleXMLElement Object
            (
                [stat_id] => 57
                [enabled] => 1
                [name] => Offensive Fumble Return TD
                [display_name] => Fum Ret TD
                [sort_order] => 1
                [position_type] => O
            )

        [11] => SimpleXMLElement Object
            (
                [stat_id] => 19
                [enabled] => 1
                [name] => Field Goals 0-19 Yards
                [display_name] => FG 0-19
                [sort_order] => 1
                [position_type] => K
            )

        [12] => SimpleXMLElement Object
            (
                [stat_id] => 20
                [enabled] => 1
                [name] => Field Goals 20-29 Yards
                [display_name] => FG 20-29
                [sort_order] => 1
                [position_type] => K
            )

    )

这是玩家统计值数组。

[stat] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [stat_id] => 4
                [value] => 0
            )

        [1] => SimpleXMLElement Object
            (
                [stat_id] => 5
                [value] => 0
            )

        [2] => SimpleXMLElement Object
            (
                [stat_id] => 6
                [value] => 0
            )

        [3] => SimpleXMLElement Object
            (
                [stat_id] => 9
                [value] => 0
            )

        [4] => SimpleXMLElement Object
            (
                [stat_id] => 10
                [value] => 0
            )

        [5] => SimpleXMLElement Object
            (
                [stat_id] => 12
                [value] => 0
            )

        [6] => SimpleXMLElement Object
            (
                [stat_id] => 13
                [value] => 0
            )

        [7] => SimpleXMLElement Object
            (
                [stat_id] => 15
                [value] => 0
            )

        [8] => SimpleXMLElement Object
            (
                [stat_id] => 16
                [value] => 0
            )

        [9] => SimpleXMLElement Object
            (
                [stat_id] => 18
                [value] => 0
            )

        [10] => SimpleXMLElement Object
            (
                [stat_id] => 57
                [value] => 0
            )

    )

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找其中一个array_intersect函数:

这些允许您比较数组并根据匹配值,匹配键或两者来查找重叠。