I am looking for a way (in PHP) to use two different arrays to get results for matching items. The arrays are sport stats. One array contains the definition of the stats and the other is the stat_id and a stat value for a player. The matching item in each array is stat_id. I am looking to get back results of only matching items in both arrays.
The first array contains stat definitions. Here is a potion of the stats definition array…
[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
)
)
Here is the player stat values array.
[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
)
)
I think you are looking for one of the array_intersect functions:
These allow you to compare arrays and find the overlap based on matching values, matching keys, or both.