I have two arrays I need to compare….
$Drink Array
(
[0] => Drink Object
(
[top_id] => 40C6-88
[name] => Pepsi
[drink_id] => E936
)
[1] => Drink Object
(
[top_id] => 46DB-9E
[name] => Orange Juice
[drink_id] => E936
)
)
[2] => Drink Object
(
[top_id] => 5J71-4F79
[name] => Dr Pepper
[drink_id] => E936
)
$DrinkItem Array
(
[0] => DrinkItem Object
(
[bottom_id] => 45BD-92DD
[name] => Diet Coke
[drink_template_id] => 3B2A-4D82
)
[1] => DrinkItem Object
(
[bottom_id] => 4A71-8F79
[name] => Orange Juice
[drink_template_id] => 3B2A-4D82
)
)
.. If the name matches one of the names in the DrinkItem Array I need to store the top_id of that item.
I was trying to do something like:
foreach ($Drink as $d) {
foreach ($DrinkItem as $item){
if ($d->name == $item->name){
$match = $d->top_id;
}
}
}
But I might be way off base here. If there’s a better approach for storing the id of the matches, or if i’m on the right track, any help is very appreciated!
You might be better off creating an index first:
If you need the entire
Drinkobject and not just thetop_id, consider using array intersection, like so: