Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:24:05+00:00 2026-05-30T18:24:05+00:00

I have two arrays I need to compare…. $Drink Array ( [0] => Drink

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:24:06+00:00Added an answer on May 30, 2026 at 6:24 pm

    You might be better off creating an index first:

    $drinkindex = array();
    
    // Note that names need to be unique.
    foreach ($Drink as $d) {
        $drinkindex[$d->name] = $d->top_id;
    }
    
    $matches = array();
    foreach ($DrinkItem as $di) {
        $match = $drinkindex[$di->name];
        if ($match!==NULL) {
            $matches[$match] = True;
        }
    }
    
    // $matches is a set of top_id.
    // If you may have duplicates and you want those represented, use a list instead.
    

    If you need the entire Drink object and not just the top_id, consider using array intersection, like so:

    function namecompare($a, $b) {
        if ($a->name === $b->name) {
            return 0;
        } else {
            return ($a->name < $b->name) ? -1: 1;
        }
    }
    
    $intersection = array_uintersect($Drink, $Drinkitem, 'namecompare');
    
    var_dump($intersection); // contains $Drink[1];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to compare the similarity between two arrays? Say I have: Base Array: [.5,0,0,0,.25,0,0,.25,0,0,0,0]
I need to do operations like compare that two number arrays have the same
I have two Ruby arrays, and I need to see if they have any
I have a two dimensional array that I need to load data into. I
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays. One contains id=>count and the other contains id=>name . I'm
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
I have two arrays: Array ( [2005] => 0 [2006] => 0 [2007] =>
I need to compare two arrays. If I test if Object1==Object2 I am testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.